繁体   English   中英

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

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

我有一些具有自定义序列化程序的实体。

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());

现在,我想通过LINQ查询此实体。

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

这返回了以下错误

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

当不使用自定义序列化程序时,不会发生此行为,所以我猜我忘记或错过了一些东西。 搜索互联网并没有产生任何我可以解决的错误。 有人可以告诉我我所缺少的吗?


Google网上论坛提供的解决方案:实施IBsonDocumentSerializer。

如果要序列化到文档,则自定义序列化程序需要实现IBsonDocumentSerializer。 如果要序列化到阵列,则需要实现IBsonArraySerializer。 这些接口允许Linq使用自定义类。

暂无
暂无

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

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