Can somebody please help? Type parameters cannot be determined from use.

c# compiler-errors dynamic-linq linq linq-to-objects

Question

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.

1
3
9/21/2011 11:22:39 AM

Accepted Answer

The extension method with this signature

public static IQueryable GroupBy(this IQueryable source, 
        string keySelector, string elementSelector, params object[] values)

isn't being invoked by this line

var result =  Items.AsQueryable().GroupBy("DeliveryDate");

as there's only one non-this argument! So the compiler thinks you're after the regular GroupBy (that takes a Func) and gets confused.

You need to supply the other selector string.

edit to add

The normal GroupBy has lots of overloads, and so Dynamic Linq needs to offer just as much flexibility. GroupBy produces 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 Orders by OrderDate produces an IEnumerable<IGrouping<DateTime, Order>>.

But we could say that we only care about the OrderId in the output, so we could give GroupBy an elementSelector to make it produce an IEnumerable<IGrouping<DateTime, int>>.

To tell Dynamic Linq to use the Item itself as the element, we use the magic string "it" as the elementSelector:

var result = Items.AsQueryable().GroupBy("DeliveryDate", "it");

This should then have the same effect as the conventional LINQ call

var result = Items.AsQueryable().GroupBy(i => i.DeliveryDate);
5
9/21/2011 10:40:55 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow