无法使用MongoDB C#2.0驱动程序查询字典项

时间:2015-04-10 18:27:22

标签: c# mongodb mongodb-.net-driver mongodb-csharp-2.0

我有一个包含字典属性的类。

    [DataMember]
    [BsonElement("QueriableParameters")]
    public Dictionary<string, string> QueriableParameters
    {
        get;
        set;
    }

我使用新的MongoDB c#2.0驱动程序似乎无法做到这一点:

var selectQuery1 = await collection.Find(s => s.QueriableParameters["UniqueLoanNumber"] == "3049793b-91eb-49d8-a5b4-7cbfd1a1bb3c").ToListAsync();

我收到此错误声明:

InnerException: System.InvalidOperationException
   HResult=-2146233079
   Message=s.QueriableParameters.get_Item("UniqueLoanNumber") is not supported.
   Source=MongoDB.Driver
   StackTrace:
        at MongoDB.Driver.Linq.Translators.PredicateTranslator.GetSerializationInfo(Expression expression)
        at MongoDB.Driver.Linq.Translators.PredicateTranslator.BuildComparisonQuery(Expression variableExpression, ExpressionType operatorType, ConstantExpression constantExpression)

请指点我正确的方向。

谢谢,

3 个答案:

答案 0 :(得分:3)

感谢克雷格提供的信息。

我想现在唯一能做的就是:

var builders = Builders<NotificationData>.Filter;
var filter = builders.Eq("QueriableParameters.UniqueLoanNumber", "theIdLookingfor");
var selectQuery = await collection.Find(filter).ToListAsync();

答案 1 :(得分:0)

目前无法做到这一点。此票证已经提交与此相关:https://jira.mongodb.org/browse/CSHARP-917

克雷格

答案 2 :(得分:0)

此问题已在最新的Mongo C#驱动程序版本中解决,即2.2.3。 链接:https://github.com/mongodb/mongo-csharp-driver