简体   繁体   English

无法从 IMongoQueryable mongodb c# 驱动程序 linq 语句上的 GroupBy 中的组中获取组项

[英]Cannot get group items from group in GroupBy on IMongoQueryable mongodb c# driver linq statement

When I try this:当我尝试这个时:

var groupedItems = _collection
    .AsQueryable()
    .GroupBy(pv => pv.ArticleNumber, (k, s) => new { k, Items = s })
    .Select(group => group.Items)
    .ToList();

I get the following exception:我得到以下异常:

    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 is the underlying type of the collection. ProductVersion是集合的基础类型。

It works when I load the collection into memory via AsEnumerable() first and then apply the GroupBy and other operations but that is not an option in my scenario.当我首先通过AsEnumerable()将集合加载到 memory 然后应用GroupBy和其他操作时,它可以工作,但这在我的场景中不是一个选项。 Is there any chance I can get the group items themselves in that linq statement on IMongoQueryable ?我有没有机会在 IMongoQueryable 上的IMongoQueryable语句中自己获取组项目?

I am using MongoDB.Driver 2.10.2.我正在使用 MongoDB.Driver 2.10.2。

I found a solution via linq provider.我通过 linq 提供程序找到了解决方案。 You have to project the grouped Items explicitly via Select() and property by property on the group result, eg:您必须通过 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.

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