Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, you'll access the current grouped value as <code>Key</code> in your Select clause:</p> <pre><code>.Select("new (Key as Group, Sum(Value) as TotalValue)"); </code></pre> <p>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 <code>DynamicClass</code>, into a static type.</p> <p>Option 1: Use reflection to access the dynamic object's <code>Group</code> and <code>TotalValue</code> properties.</p> <p>Option 2: Use compiled expression trees for lightweight code generation to access the <code>Group</code> and <code>TotalValue</code> properties.</p> <p>Option 3: Modify the Dynamic library to support a strongly-typed result. This turns out to be rather simple:</p> <ol> <li><p>In <code>ExpressionParser.Parse()</code>, capture the type argument in a private field:</p> <pre><code>private Type newResultType; public Expression Parse(Type resultType) { newResultType = resultType; int exprPos = token.pos; // ... </code></pre></li> <li><p>Near the end of <code>ExpressionParser.ParseNew()</code>, we'll try to use <code>newResultType</code> before defaulting to a dynamic type:</p> <pre><code>Expression ParseNew() { // ... NextToken(); Type type = newResultType ?? DynamicExpression.CreateClass(properties); MemberBinding[] bindings = new MemberBinding[properties.Count]; for (int i = 0; i &lt; bindings.Length; i++) bindings[i] = Expression.Bind(type.GetProperty(properties[i].Name), expressions[i]); return Expression.MemberInit(Expression.New(type), bindings); } </code></pre></li> <li><p>Finally, we need a strongly typed version of <code>Select()</code>:</p> <pre><code>public static IQueryable&lt;TResult&gt; Select&lt;TResult&gt;(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&lt;TResult&gt;( Expression.Call( typeof(Queryable), "Select", new Type[] { source.ElementType, typeof(TResult) }, source.Expression, Expression.Quote(lambda))); } </code></pre> <p>The only changes from the original <code>Select()</code> are places we reference <code>TResult</code>.</p></li> </ol> <p>Now we just need a named type to return:</p> <pre><code> public class Result { public string Group { get; set; } public double TotalValue { get; set; } } </code></pre> <p>And your updated query will look like this:</p> <pre><code> IQueryable&lt;Result&gt; res = table1.AsQueryable() .GroupBy(groupbyvalue, "it") .Select&lt;Result&gt;("new (Key as Group, Sum(Value) as TotalValue)"); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload