Dima Grigoriev Dima Grigoriev - 1 month ago 15
C# Question

Element 'group' does not match any field or property of class Gillie.JobCenter.Domain.KeyValueEntity

can someone help me? i spend a lot of time to resolving this problem and nothing

this is structure of mongodb collection:
enter image description here

this is my entity and helped objects:

public class Course
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public ObjectId Id { get; set; }

[BsonRepresentation(BsonType.ObjectId)]
[BsonElement("_etag")]
public ObjectId Etag { get; set; }

[BsonElement("group")]
public KeyValueEntity<string> Group { get; set; }

[BsonElement("values")]
public GroupValuesCourse[] Values { get; set; }
}

public class KeyValueEntity<T>
{
[BsonElement("key")]
[JsonProperty("key")]
public string Key { get; set; }

[BsonElement("value")]
[JsonProperty("value")]
public T Value { get; set; }

public override string ToString()
{
return Value?.ToString();
}
}
public class GroupValuesCourse
{
[BsonElement("group")]
public KeyValueEntity<string> GroupKeys { get; set; }

[BsonElement("values")]
public KeyValueEntity<string>[] ValueKeys { get; set; }
}


And my repository

public async Task<IEnumerable<KeyValueEntity<string>>> GetAllForGroupAsync(string groupName, string subGroupName)
{
return await mongoDbContext.MongoDataBase.GetCollection<Course>("courses").AsQueryable()
.Where(courseGroup => courseGroup.Group.Value == groupName)
.SelectMany(courseGroups => courseGroups.Values)
.Where(subgroup => subgroup.GroupKeys.Value == subGroupName)
.SelectMany(groups => groups.ValueKeys)
.OrderBy(value => value.Value)
.ToListAsync();
}


And after execution this tell me that:System.FormatException: Element 'group' does not match any field or property of class Gillie.JobCenter.Domain.KeyValueEntity`1[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].
But i'm sure that mapping is correct. Can someone help me to find mistake?

Answer

I've implemented by own using this little hack. I've used Select and First instead of SelectMany, and it's work now. As i'm understood mongodbDriver don't support hard quties like this. So it's my result

    public async Task<IEnumerable<KeyValueEntity<string>>> GetAllForGroupAsync(string groupName, string subGroupName)
    {
        return async coursesCollection.AsQueryable()
             .Where(courseGroup => courseGroup.Group.Value == groupName)
                .SelectMany(courseGroups => courseGroups.Values)
                    .Where(subGroup => subGroup.GroupKeys != null)
                    .Where(subgroup => subgroup.GroupKeys.Value == subGroupName)
                        .Select(groups => groups.ValueKeys)
                        .FirstAsync();
    }
Comments