简体   繁体   English

如何结合使用LINQ和自定义序列化器? (MONGODB C#)

[英]How to use/enable LINQ combined with custom serializer? (MONGODB C#)

I have a few entities that have custom serializers. 我有一些具有自定义序列化程序的实体。

public class EntitySerializer : BsonBaseSerializer, IBsonIdProvider
{
   public override object Deserialize(MongoDB.Bson.IO.BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options){
      ...
      bsonReader.ReadName(); // _id
      ObjectId id = bsonReader.ReadObjectId();
      ...
}
public override void Serialize(MongoDB.Bson.IO.BsonWriter bsonWriter, Type nominalType, object value, IBsonSerializationOptions options)
{
     ...
     bsonWriter.WriteObjectId("_id", ent.Id);
     ...
    }
}

....

BsonSerializer.RegisterSerializer(typeof(Entity), new EntitySerializer());

Now I would like to query this entity through LINQ. 现在,我想通过LINQ查询此实体。

GetEntityCollection().AsQueryable<Entity>().Where(d=>d.Id==new ObjectId("51b8939d3f92b82db4ad1db0"))

This returns me the following error 这返回了以下错误

NotSupportedException: Unable to determine the serialization information for the expression: d.Id.

This behavior does not occur when not using a custom serializer so I am guessing I forgot or missed something. 当不使用自定义序列化程序时,不会发生此行为,所以我猜我忘记或错过了一些东西。 Searching the internet did not yield me anything that I could resolve the error with. 搜索互联网并没有产生任何我可以解决的错误。 Could someone please show me what I am missing? 有人可以告诉我我所缺少的吗?


Solution given at Google Groups: implement IBsonDocumentSerializer. Google网上论坛提供的解决方案:实施IBsonDocumentSerializer。

If you are serializing to a document, your custom serializer needs to implement IBsonDocumentSerializer. 如果要序列化到文档,则自定义序列化程序需要实现IBsonDocumentSerializer。 IF it is serializing to an array, it needs to implement IBsonArraySerializer. 如果要序列化到阵列,则需要实现IBsonArraySerializer。 These interfaces allow Linq to work with custom classes. 这些接口允许Linq使用自定义类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM