[英]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.