Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to create C# LambdaExpression that returns anonymous type for SelectMany resultSelector
    primarykey
    data
    text
    <p>I'm building a dynamic query that can have <strong>n</strong> of <code>Where</code> method calls and <strong>n</strong> of <code>SelectMany</code> calls dependent upon user input. For example I may have:</p> <pre class="lang-cs prettyprint-override"><code>var qZ = entityContext.TableA .SelectMany(a=&gt;a.TableB, (a,t)=&gt;new{a,t} ) .Where(a=&gt;a.t.FieldID==21) .Where(a=&gt; EntityFunctions.Left(a.t.Value,1)=="p") .SelectMany(a=&gt;a.a.TableC, (a,t)=&gt;new{a,t} ) .Where(a=&gt;a.t.FieldID==22) .Where(a=&gt; a.a.t.Value=="Peter" &amp;&amp; a.t.Value=="Pan") .Where(a=&gt; a.a.a.TypeID==3) .Select(a=&gt; new{ a.a.a.ItemID } ).Distinct(); </code></pre> <p>In the method I'm writing, I use helper methods that return an <code>IQueryable</code> as seen in the return line below. </p> <pre class="lang-cs prettyprint-override"><code>return query.Provider.CreateQuery( Expression.Call(typeof(Queryable), "Where", new Type[] {query.ElementType}, query.Expression, predicateLambda) ); </code></pre> <p>I'm able to create LambdaExpressions for all of the various query <em>attribute-value</em> pairs required, but I am unable to create one for the <code>resultSelector</code> of <code>Queryable.SelectMany</code>.</p> <p>How can we create <code>(a,t) =&gt; new{a=a, t=t}</code> in an expression tree? Or How do we accomplish the same result as the .SelectMany above using Expression.Call like below?</p> <pre class="lang-cs prettyprint-override"><code>Expression.Call(typeof(Queryable), "SelectMany", ????????, ???????? ); </code></pre> <p>I've tried using the <code>SelectMany</code> overload that doesn't require the <code>resultSelector</code> which works to some degree, however, I don't know how to reference the properties of t in subsequent method calls.</p> <p>I've found this lambda expression (<code>(a,t) =&gt; new{a=a, t=t}</code>) associated with <code>SelectMany</code> all over the web, but I can't find any example of how to convert it to an expression tree.</p> <p>UPDATE: Let's reframe the question. I can pass the lambda like this</p> <pre class="lang-cs prettyprint-override"><code>var q = entityContext.TableA.AsQueryable(); var q1 = Queryable.SelectMany(q, a =&gt; a.TableB, (a, t) =&gt; new { a = a, t = t }); var q2 = Queryable.Where(q1,a=&gt;a.t.FieldID==22); </code></pre> <p>That works, however, since I don't know ahead of time how many SelectMany need to be called and since each call changes to anonymous type of the IQueriable, is there a way to cast (and re-cast) the anonymous type to a single variable? This way I can loop through and apply whatever method necessary to the variable and then enumerate to get the results once the query is built. Something like:</p> <pre class="lang-cs prettyprint-override"><code>var q = entityContext.TableA..AsQueryable(); q = Queryable.SelectMany(q, a =&gt; a.TableB, (a, t) =&gt; new { a = a, t = t }); q = Queryable.Where(q,a=&gt;a.t.FieldID==22); </code></pre> <p>(BTW: This doesn't work)</p>
    singulars
    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.
 

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