Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I combine two Member Expression Trees?
    primarykey
    data
    text
    <p>I'm trying to combine the following expressions into a single expression: item => item.sub, sub => sub.key to become item => item.sub.key. I need to do this so I can create an OrderBy method which takes the item selector separately to the key selector. This can be accomplished using one of the overloads on OrderBy and providing an <code>IComparer&lt;T&gt;</code>, but it won't translate to SQL.</p> <p>Following is a method signature to further clarify what I am trying to achive, along with an implementation that doesn't work, but should illustrate the point.</p> <pre><code> public static IOrderedQueryable&lt;TEntity&gt; OrderBy&lt;TEntity, TSubEntity, TKey&gt;( this IQueryable&lt;TEntity&gt; source, Expression&lt;Func&lt;TEntity, TSubEntity&gt;&gt; selectItem, Expression&lt;Func&lt;TSubEntity, TKey&gt;&gt; selectKey) where TEntity : class where TSubEntity : class { var parameterItem = Expression.Parameter(typeof(TEntity), "item"); ... some magic ... var selector = Expression.Lambda(magic, parameterItem); return (IOrderedQueryable&lt;TEntity&gt;)source.Provider.CreateQuery( Expression.Call(typeof(Queryable), "OrderBy", new Type[] { source.ElementType, selector.Body.Type }, source.Expression, selector )); } </code></pre> <p>which would be called as:</p> <pre><code>.OrderBy(item =&gt; item.Sub, sub =&gt; sub.Key) </code></pre> <p>Is this possible? Is there a better way? The reason I want an OrderBy method that works this way is to support a complex key selection expression that applies to many entities, though they are exposed in different ways. Also, I'm aware of a way to do this using String representations of deep properties, but I'm trying to keep it strongly typed.</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