Note that there are some explanatory texts on larger screens.

plurals
  1. POconstruct expression tree in run time
    primarykey
    data
    text
    <p>is it possible to construct MethodCallExpression (by using Expression.Call), when this call is NESTED? so, in design time, the actual sequnce (IQueryable or IEnumerable) which the call should be run against, is unknown?</p> <p>Example:<br> let's say this is the query:<br> <code> var result = data.GroupBy(x =&gt; x.Name).Select(grouping =&gt; grouping.OrderByDescending(x =&gt; x.Date).Take(1).FirstOrDefault()).ToList(); </code> </p> <p>when data is a list of objects contains:Name,Date properties.<br> How do i constructs this query block: <code> grouping =&gt; grouping.OrderByDescending(x =&gt; x.Date) </code> using Expression.call?</p> <p>thanks</p> <p>My Answer:<br> I managed to sort this out.<br> Aducci gave me the direction, thnks! the key was to use ParameterExpression and not an actual collection instance.<br> From here, the sulotion is just around the corner: complie the Expression>, and call the compiled result with the actual collection.<br> this was very helpful: <a href="https://stackoverflow.com/questions/1367353/adding-a-method-call-to-a-linq-expression-whilst-remaining-an-full-expression">MethodCallExpression -> compiled expression </a><br> and also : <a href="https://stackoverflow.com/questions/3885516/expression-call-calling-linq-extension-firstordefault-where">link</a> </p> <p>i'm adding my final code: </p> <pre><code> Expression&lt;Func&lt;Data,DateTime&gt;&gt; lmbd = x =&gt; x.Date; ParameterExpression par = Expression.Parameter(typeof(IQueryable&lt;Data&gt;),"pname"); ParameterExpression[] parameters = new ParameterExpression[] {par}; MethodCallExpression method = Expression.Call(typeof(Queryable),"OrderBy",new Type[]{typeof(Data),typeof(DateTime)},par, lmbd); var lambaExpression = Expression.Lambda&lt;Func&lt;IQueryable&lt;Data&gt;, IQueryable&lt;Data&gt;&gt;&gt;(method, par); var compiled = lambaExpression.Compile(); </code></pre> <p>Thank you all!</p>
    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.
 

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