【问题标题】:How can I query a mongodb document with Dictionary<Enum, int>?如何使用 Dictionary<Enum, int> 查询 mongodb 文档?
【发布时间】:2018-03-25 21:47:10
【问题描述】:

使用 mongodb-csharp 驱动 (https://github.com/mongodb/mongo-csharp-driver)

我有以下类和枚举:

public enum PropertyType {
  Unknown,
  Age,
  Weight,
  Gender
}
public class Data {
  public Dictionary<PropertyType, Int32> Props {get;set;}
}

我可以读取和保存如下所示的数据。

{
  Props: {
    1: 28,
    2: 220,
    3: 0
  }
}

我不能做的是用 Props[PropertyType.Age] == 28 查询“数据”.. 见下面的代码:

var data  = from d in collection where d.Props[PropertyType.Age] == 28 select d;

我得到的错误是:

System.InvalidOperationException: 'data.Props.get_Item(PropertyType.Age) 不受支持。'

帮助我obiwan kenobi你我唯一的希望。

【问题讨论】:

    标签: mongodb mongodb-.net-driver


    【解决方案1】:

    你试过了吗

    var data  = from d in collection where d.Props.Any(x => x.Key.Equals(PropertyType.Age) && x.Value == 28) select d;
    

    【讨论】:

    • System.NotSupportedException: 'The expression tree is not supported: {document}{Props}'
    • 我也需要这个问题的答案
    • 不知道有没有人找到这个问题的答案?
    猜你喜欢
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-10
    • 1970-01-01
    • 2020-09-24
    相关资源
    最近更新 更多