First, you’ll access the current grouped value as Key
in your Select clause:
.Select("new (Key as Group, Sum(Value) as TotalValue)");
That should make your query work. The harder question is how to turn the returned objects, which will have a dynamically generated type that inherits from DynamicClass
, into a static type.
Option 1: Use reflection to access the dynamic object’s Group
and TotalValue
properties.
Option 2: Use compiled expression trees for lightweight code generation to access the Group
and TotalValue
properties.
Option 3: Modify the Dynamic library to support a strongly-typed result. This turns out to be rather simple:
-
In
ExpressionParser.Parse()
, capture the type argument in a private field:private Type newResultType; public Expression Parse(Type resultType) { newResultType = resultType; int exprPos = token.pos; // ...
-
Near the end of
ExpressionParser.ParseNew()
, we’ll try to usenewResultType
before defaulting to a dynamic type:Expression ParseNew() { // ... NextToken(); Type type = newResultType ?? DynamicExpression.CreateClass(properties); MemberBinding[] bindings = new MemberBinding[properties.Count]; for (int i = 0; i < bindings.Length; i++) bindings[i] = Expression.Bind(type.GetProperty(properties[i].Name), expressions[i]); return Expression.MemberInit(Expression.New(type), bindings); }
-
Finally, we need a strongly typed version of
Select()
:public static IQueryable<TResult> Select<TResult>(this IQueryable source, string selector, params object[] values) { if (source == null) throw new ArgumentNullException("source"); if (selector == null) throw new ArgumentNullException("selector"); LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(TResult), selector, values); return source.Provider.CreateQuery<TResult>( Expression.Call( typeof(Queryable), "Select", new Type[] { source.ElementType, typeof(TResult) }, source.Expression, Expression.Quote(lambda))); }
The only changes from the original
Select()
are places we referenceTResult
.
Now we just need a named type to return:
public class Result
{
public string Group { get; set; }
public double TotalValue { get; set; }
}
And your updated query will look like this:
IQueryable<Result> res = table1.AsQueryable()
.GroupBy(groupbyvalue, "it")
.Select<Result>("new (Key as Group, Sum(Value) as TotalValue)");