C# 元素';集团';不匹配Gillie.JobCenter.Domain.KeyValueEntity类的任何字段或属性

C# 元素';集团';不匹配Gillie.JobCenter.Domain.KeyValueEntity类的任何字段或属性,c#,mongodb,linq,C#,Mongodb,Linq,有人能帮我吗?我花了很多时间来解决这个问题,什么都没有 这是mongodb集合的结构: 这是我的实体和帮助对象: public class Course { [BsonId] [BsonRepresentation(BsonType.ObjectId)] public ObjectId Id { get; set; } [BsonRepresentation(BsonType.ObjectId)] [BsonElement("_etag")]

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

这是mongodb集合的结构:

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

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; }
}
公共课
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
公共对象Id{get;set;}
[BsonRepresentation(BsonType.ObjectId)]
[b单一元素(“_etag”)]
公共ObjectId Etag{get;set;}
[b单一元素(“集团”)]
public KeyValueEntity组{get;set;}
[b单一元素(“值”)]
公共组值rse[]值{get;set;}
}
公共类KeyValueEntity
{
[b单一元素(“键”)]
[JsonProperty(“密钥”)]
公共字符串密钥{get;set;}
[b单一元素(“价值”)]
[JsonProperty(“价值”)]
公共T值{get;set;}
公共重写字符串ToString()
{
返回值?.ToString();
}
}
公共类组价值观
{
[b单一元素(“集团”)]
public KeyValueEntity组键{get;set;}
[b单一元素(“值”)]
public KeyValueEntity[]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();
    }
公共异步任务GetAllForGroupAsync(字符串groupName,字符串SubName) { return wait wait mongoDbContext.MongoDataBase.GetCollection(“课程”).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:元素“group”与Gillie.JobCenter.Domain.KeyValueEntity'1[[System.String,System.Private.CoreLib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e]]类的任何字段或属性都不匹配。
但我确信映射是正确的。有人能帮我找出错误吗?

好像是课程。某些课程的组为空。也许会以某种方式过滤掉。我添加了一个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();
}
公共异步任务GetAllForGroupAsync(字符串groupName,字符串SubName) { return wait wait mongoDbContext.MongoDataBase.GetCollection(“课程”).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(); }
似乎是课程。对于某些课程,组为空。也许会以某种方式过滤掉。我添加了一个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();
}
公共异步任务GetAllForGroupAsync(字符串groupName,字符串SubName) { return wait wait mongoDbContext.MongoDataBase.GetCollection(“课程”).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(); }
我已经用这个小技巧自己实现了。我使用了Select and 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();
    }
公共异步任务GetAllForGroupAsync(字符串groupName,字符串SubName) { 返回异步CourseCollection.AsQueryable() .Where(courseGroup=>courseGroup.Group.Value==groupName) .SelectMany(courseGroups=>courseGroups.Values) .Where(subGroup=>subGroup.GroupKeys!=null) .Where(subgroup=>subgroup.GroupKeys.Value==subGroupName) .选择(组=>groups.ValueKeys) .FirstAsync(); }
我已经用这个小技巧自己实现了。我使用了Select and 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();
    }
公共异步任务GetAllForGroupAsync(字符串groupName,字符串SubName) { 返回异步CourseCollection.AsQueryable() .Where(courseGroup=>courseGroup.Group.Value==groupName) .SelectMany(courseGroups=>courseGroups.Values) .Where(subGroup=>subGroup.GroupKeys!=null) .Where(subgroup=>subgroup.GroupKeys.Value==subGroupName) .选择(组=>groups.ValueKeys) .FirstAsync(); }
这仍然是实际问题。这仍然是实际问题。请不要破坏您的帖子。@Dimargoriev不,您没有。通过在堆栈溢出上发布此内容,您同意公开许可此内容,从而使您在此发布上的声明无效。不,您不同意。有些用户花时间和精力写下了体面的答案。你可以随时要求将此帖子与你的帐户分离;请使用此表单。请不要破坏您的帖子。@Dimargoriev不,您不需要。通过在堆栈溢出上发布此内容,您同意公开许可此内容,从而使您在此发布上的声明无效。不,您不同意。有些用户花时间和精力写下了体面的答案。你可以随时要求将此帖子与你的帐户分离;请使用此表单。