[英]Cannot get group items from group in GroupBy on IMongoQueryable mongodb c# driver linq statement
当我尝试这个时:
var groupedItems = _collection
.AsQueryable()
.GroupBy(pv => pv.ArticleNumber, (k, s) => new { k, Items = s })
.Select(group => group.Items)
.ToList();
我得到以下异常:
System.ArgumentException: Value type of serializer is ProductVersion[] and does not match member type System.Collections.Generic.IEnumerable`1[[ProductVersion, ProductService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. (Parameter 'serializer')
at MongoDB.Bson.Serialization.BsonMemberMap.SetSerializer(IBsonSerializer serializer)...
ProductVersion
是集合的基础类型。
当我首先通过AsEnumerable()
将集合加载到 memory 然后应用GroupBy
和其他操作时,它可以工作,但这在我的场景中不是一个选项。 我有没有机会在 IMongoQueryable 上的IMongoQueryable
语句中自己获取组项目?
我正在使用 MongoDB.Driver 2.10.2。
我通过 linq 提供程序找到了解决方案。 您必须通过 Select() 和按属性在组结果上显式投影分组的项目,例如:
var groupedItems = _collection
.AsQueryable()
.GroupBy(pv => pv.ArticleNumber, (key, group) => new
{
key,
Items = group.Select(groupItem => new ProductVersion { PropertyA = groupItem.PropertyA, ... })
})
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.