无法在 IMongoQueryable mongodb c# 驱动程序 linq 语句上从 GroupBy 中的组中获取组项目
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()
将集合加载到内存中然后应用 GroupBy
和其他操作时,它会起作用,但在我的场景中这不是一个选项。
我有没有机会在 IMongoQueryable
?
的 linq 语句中获取组项本身
我正在使用 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, ... })
})
...
当我尝试这个时:
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()
将集合加载到内存中然后应用 GroupBy
和其他操作时,它会起作用,但在我的场景中这不是一个选项。
我有没有机会在 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, ... })
})
...