元素'组'与Gillie.JobCenter.Domain.KeyValueEntity类的任何字段或属性都不匹配

时间:2016-11-03 12:13:44

标签: c# mongodb linq

有人能帮帮我吗?我花了很多时间来解决这个问题而没有什么

这是mongodb集合的结构: enter image description here

这是我的实体和帮助对象:

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; }
}

和我的资料库

    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();
    }

执行完毕后告诉我:System.FormatException:Element&#39; group&#39;不匹配类Gillie.JobCenter.Domain.KeyValueEntity`1 [[System.String,System.Private.CoreLib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e]]的任何字段或属性。 但我确信映射是正确的。有人能帮我找错吗?

2 个答案:

答案 0 :(得分:2)

对于某些课程,似乎Course.Group为null。或许以某种方式过滤掉。我添加了一个where子句来摆脱它们作为一个例子。

public async Task<IEnumerable<KeyValueEntity<string>>> GetAllForGroupAsync(string groupName, string subGroupName)
{
    return await mongoDbContext.MongoDataBase.GetCollection<Course>("courses").AsQueryable()
        .Where(courseGroup => courseGroup.Group != null)
        .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();
}

答案 1 :(得分:1)

我已经使用这个小黑客自己实现了。我使用Select和First而不是SelectMany,现在它已经工作了。据我所知,mongodbDriver不支持像这样的硬问题。所以这是我的结果

    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();
    }