I am trying to use Dynamic Linq (the one published by ScottGu) to do a GroupBy (lambda expression). I have an object. And i am doing AsQueryable. THis is what i have..
var result = Items.AsQueryable().GroupBy("DeliveryDate");
but it gives me an error saying that arguments cannot be inferred from usage. I think its not looking at the right OrderBy.
Items is a List<Stock>.
I tried adding the < > to GroupBy but i think i need to pass more than 1 item. I am a little confused.
The field that i am passing in (via a string) could be any type, in this example it is a DateTime.
If I do my standard LINQ it does work. i.e.:
var result = Items.AsQueryable().GroupBy( x => x.DeliveryDate);
Here is the extension method (from ScottGu):
public static IQueryable GroupBy(this IQueryable source, string keySelector, string elementSelector, params object[] values) {
if (source == null) throw new ArgumentNullException("source");
if (keySelector == null) throw new ArgumentNullException("keySelector");
if (elementSelector == null) throw new ArgumentNullException("elementSelector");
LambdaExpression keyLambda = DynamicExpression.ParseLambda(source.ElementType, null, keySelector, values);
LambdaExpression elementLambda = DynamicExpression.ParseLambda(source.ElementType, null, elementSelector, values);
return source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "GroupBy",
new Type[] { source.ElementType, keyLambda.Body.Type, elementLambda.Body.Type },
source.Expression, Expression.Quote(keyLambda), Expression.Quote(elementLambda)));
}
I have the using in the project using System.Linq.Dynamic; – and i am using dynamic OrderBy in another method, so i know it should be able to see it.
I am a little confused, it seems using dynamic LINQ it doesn’t understand what object I am using.
The extension method with this signature
isn’t being invoked by this line
as there’s only one non-
thisargument! So the compiler thinks you’re after the regularGroupBy(that takes aFunc) and gets confused.You need to supply the other
selectorstring.edit to add
The normal
GroupByhas lots of overloads, and so Dynamic Linq needs to offer just as much flexibility.GroupByproduces a sequence of groupings each of which has a key and a sequence of elements. If, as in your conventional LINQ call, no mention is made of the elements, the source objects themselves are used as elements.For example, using some hypothetical classes, grouping
OrdersbyOrderDateproduces anIEnumerable<IGrouping<DateTime, Order>>.But we could say that we only care about the
OrderIdin the output, so we could giveGroupByanelementSelectorto make it produce anIEnumerable<IGrouping<DateTime, int>>.To tell Dynamic Linq to use the
Itemitself as the element, we use the magic string"it"as theelementSelector:This should then have the same effect as the conventional LINQ call