C# MongoDB c“驱动程序-执行LINQ”;任何;在序列化字典中
我有一个带有一些属性的文档类型,其中一个属性是DictionaryC# MongoDB c“驱动程序-执行LINQ”;任何;在序列化字典中,c#,linq,mongodb,mongodb-.net-driver,mongodb-query,C#,Linq,Mongodb,Mongodb .net Driver,Mongodb Query,我有一个带有一些属性的文档类型,其中一个属性是Dictionary 序列化和反序列化似乎工作得很好(我可以搜索、执行CRUD操作等)。问题是,我正在尝试编写一个方法来查找字典键中包含特定值(id)的所有该类型的对象 尝试的查询: var objectCollection = db.GetCollection<MyClass>("MyClass"); var query = Query<MyClass>.Where(m => m.MyDictionary.Any(k
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; }
}
我怀疑问题与c#驱动程序的默认序列化程序不支持这一事实有关。有解决办法吗
我还尝试对
Dictionary.Keys
集合上的id
字符串执行“Contains”,但这会产生NotSupportedException:无法确定表达式的序列化信息:m.Dictionary.Keys。
我自己解决了这个问题
在MyClass
中添加了注释,以指定如何序列化词典(有关详细信息)
这将搜索“我的收藏”中的条目,其中字典包含id
作为其键之一
其他相关链接:
NotSupportedException:无法确定表达式:Enumerable.ToList(m.Dictionary.Keys)的序列化信息。
。
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.
[BsonElement("Dictionary")]
[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)] // Added This!
public Dictionary<string, string> Dictionary { get; set; }
var query = Query.ElemMatch("Dictionary", Query.EQ("k", id));