【问题标题】:Element 'group' does not match any field or property of class Gillie.JobCenter.Domain.KeyValueEntity元素“组”不匹配 Gillie.JobCenter.Domain.KeyValueEntity 类的任何字段或属性
【发布时间】:2017-01-23 20:22:02
【问题描述】:

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

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

还有我的存储库

    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 '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,文化=中性,PublicKeyToken=7cec85d7bea7798e]]。 但我确信映射是正确的。有人可以帮我找出错误吗?

【问题讨论】:

  • 请不要破坏您的帖子。
  • @DimaGrigoriev 不,你没有。通过在 Stack Overflow 上发布此内容,您同意公开许可此内容,从而使您在此帖子上的声明无效。
  • 不,你没有。有些用户花时间和精力写出体面的答案。
  • 您可以随时要求将此帖子与您的帐户解除关联;为此使用Contact Us 表单。

标签: c# mongodb linq


【解决方案1】:

对于某些课程,Course.Group 似乎为空。也许以某种方式过滤掉。作为示例,我添加了一个 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();
}

【讨论】:

    【解决方案2】:

    我已经使用这个小技巧自己实现了。我使用了 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();
        }
    

    【讨论】:

    • 这仍然是一个实际问题
    猜你喜欢
    • 2012-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    • 2016-07-25
    相关资源
    最近更新 更多