Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MongoDB c“驱动程序-执行LINQ”;任何;在序列化字典中_C#_Linq_Mongodb_Mongodb .net Driver_Mongodb Query - Fatal编程技术网

C# MongoDB c“驱动程序-执行LINQ”;任何;在序列化字典中

C# 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

我有一个带有一些属性的文档类型,其中一个属性是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; }
}
我怀疑问题与c#驱动程序的默认序列化程序不支持这一事实有关。有解决办法吗


我还尝试对
Dictionary.Keys
集合上的
id
字符串执行“Contains”,但这会产生
NotSupportedException:无法确定表达式的序列化信息:m.Dictionary.Keys。
我自己解决了这个问题

MyClass
中添加了注释,以指定如何序列化词典(有关详细信息)

这将搜索“我的收藏”中的条目,其中字典包含
id
作为其键之一

其他相关链接:


在放入linq之前,您可以先尝试将m.MyDictionary.Keys转换为列表吗?var listOfKeys=m.Dictionary.Keys.ToList();否:
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));