【发布时间】:2014-05-19 12:03:06
【问题描述】:
我有一个带有一些属性的文档类型,其中之一是 Dictionary。
序列化和 去序列化 似乎工作正常(我可以搜索、执行 CRUD 操作等)。问题是我正在尝试编写一个方法来查找该类型的所有对象,其中字典在其键中包含特定值(id)。
尝试查询:
var objectCollection = db.GetCollection<MyClass>("MyClass");
var query = Query<MyClass>.Where(m => m.MyDictionary.Any(k => k.Key == id));
班级:
public class MyClass
{
public ObjectId Id { get; set; }
// ...
[BsonElement("Dictionary")]
public Dictionary<string, string> MyDictionary { get; set; }
}
...但是我在构建查询时遇到了这个异常:
Any requires that the serializer specified for Dictionary support items by implementing
MongoDB.Bson.Serialization.IBsonArraySerializer and returning a non-null result.
MongoDB.Bson.Serialization.Serializers.DictionarySerializer`2[System.String,System.String]
is the current serializer.
我怀疑这个问题与 c# 驱动程序的默认序列化程序不支持这一事实有关。有解决办法吗?
我还尝试对Dictionary.Keys 集合上的id 字符串执行“包含”,但这给出了NotSupportedException: Unable to determine the serialization information for the expression: m.Dictionary.Keys.
【问题讨论】:
-
也许您可以先尝试将 m.MyDictionary.Keys 转换为列表,然后再放入 linq? var listOfKeys = m.Dictionary.Keys.ToList();
-
不是:
NotSupportedException: Unable to determine the serialization information for the expression: Enumerable.ToList<String>(m.Dictionary.Keys).也是。
标签: c# linq mongodb mongodb-.net-driver mongodb-query