简体   繁体   English

C#MongoDB和Projection。 出现序列化异常失败

[英]C# MongoDB & Projection. Fails with serialization Exception

Let's say I have a collection like : 假设我有一个类似的收藏:

 {
 id: "1"
 name: "collection 1"
 properties: "Some properties."
 }

With a class representation as 以类表示为

[BsonIgnoreExtraElements]
public class InfoPOCO {
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; }

    [BsonElement("name")]
    public string Name { get; set; }

    [BsonElement("properties")]
    public string Properties { get; set; }
 } 

Now if am going to create a Projection like 现在,如果要创建一个像

Builders<InfoPOCO>.Projection.Include(_ => new{_.Name});

And call it with other params ( That is working fine without projection ) 并与其他参数一起调用(无需投影即可正常工作)

return GetDataBase().GetCollection<InfoPOCO>(collectionName).Find(Expr).
Project<InfoPOCO>(projectionDefinition).Skip(Offset).Limit(Limit).Sort(sort).ToList<InfoPOCO>()

Then I get the following error: 然后我得到以下错误:

System.InvalidOperationException : Unable to determine the serialization information for
_ => new <>f__AnonymousType2`1


 Result StackTrace: 
at MongoDB.Driver.ExpressionFieldDefinition`1.Render(IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.SingleFieldProjectionDefinition`1.Render(IBsonSerializer`1 sourceSerializer, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.KnownResultTypeProjectionDefinitionAdapter`2.Render(IBsonSerializer`1 sourceSerializer, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOperation[TProjection](FilterDefinition`1 filter, FindOptions`2 options)
   at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](IClientSessionHandle session, FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.<>c__DisplayClass35_0`1.<FindSync>b__0(IClientSessionHandle session)
   at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSession[TResult](Func`2 func, CancellationToken cancellationToken)
   at MongoDB.Driver.MongoCollectionImpl`1.FindSync[TProjection](FilterDefinition`1 filter, FindOptions`2 options, CancellationToken cancellationToken)
   at MongoDB.Driver.FindFluent`2.ToCursor(CancellationToken cancellationToken)
   at MongoDB.Driver.IAsyncCursorSourceExtensions.ToList[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)

What could be the reason for it, when am not using projection, it is fetching the entire collection fine. 可能的原因是,当不使用投影时,它会提取整个集合。 I don't know if I need to map the classes? 我不知道是否需要映射类吗? (Thought Auto Map would take it) . (认为​​自动地图会接受)。 I tried making the constructor in the class too. 我也尝试在类中构造构造函数。 But still the same error. 但是还是一样的错误。 Any help would be highly appreciated. 任何帮助将不胜感激。 Thank you ! 谢谢 !

I finally understood! 我终于明白了! Yeah, I know, kinda slow, duh. 是的,我知道,有点慢,du。

The issue is that when I was trying to map it like : 问题是,当我尝试将其映射为:

Builders<InfoPOCO>.Projection.Include(_ => new{_.Name, _.Properties});

It's a strongly typed expression and the result is mapped to an anonymous type. 这是一个强类型的表达式,结果被映射到一个匿名类型。 If am using forEach at the same time as am fetching it, then I can get those values. 如果在获取时同时使用forEach,那么我可以获得这些值。 So the solution I am using right now is simple. 因此,我现在使用的解决方案很简单。 I changed my projection to: 我将投影更改为:

Builders<InfoPOCO>.Projection.Include(_ => _.Name).
                              Include(_ => _.Properties);

This works! 这可行! Am not saying its the perfect solution or explanation but I see a lot of people struggling with it, so just mentioning a workaround. 并不是说它是完美的解决方案或解释,但我看到很多人都在为之苦苦挣扎,所以只提一个解决方法。 Hope it helps someone like me from going through hours of googling! 希望它可以帮助像我这样的人避免经过数小时的谷歌搜索!

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

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