'No generic method 'GroupBy' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments

Viewed 60

I'm trying to dynamically use reflection to do a GroupBy after a SelectMany:

var origType = typeof(CustomerCases);     
var parameter = Expression.Parameter(origType, "x");
var propExpr = Expression.Property(par, "Customer");
var firstGenType = typeof(Customer);
var resultType = typeof(IGrouping<,>).MakeGenericType(new [] {firstGenType, origType});

var lambda = Expression.Lambda(Expression.Convert(propExpr, resultType), parameter);

var groupbyExpression = Expression.Call(
                typeof(Queryable), 
                nameof(Queryable.GroupBy), 
                new [] { origType, firstGenType },
                querySelectMany.Expression, 
                lambda);

However, when trying to set the groupbyExpression, it throws an exception:

An exception of type 'System.InvalidOperationException' occurred in System.Linq.Expressions.dll but was not handled in user code: 'No generic method 'GroupBy' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic. '

Any ideas?

0 Answers
Related