2

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.

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. Is there any chance I can get the group items themselves in that linq statement on IMongoQueryable?

I am using MongoDB.Driver 2.10.2.

sprengo
  • 138
  • 9

1 Answers1

2

I found a solution via linq provider. You have to project the grouped Items explicitly via Select() and property by property on the group result, e.g.:

var groupedItems = _collection
    .AsQueryable()
    .GroupBy(pv => pv.ArticleNumber, (key, group) => new
    {
        key,
        Items = group.Select(groupItem => new ProductVersion { PropertyA = groupItem.PropertyA, ... })
    })
...
sprengo
  • 138
  • 9