Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can do this by using an open generic type definition, and then making the specific type from that:</p> <pre><code>typeof(Func&lt;,&gt;).MakeGenericType(typeof(int), orderType); </code></pre> <p>However, what you're trying to do (calling <code>Lambda&lt;TDelegate&gt;</code>) is not directly possible. You must call <code>Lambda</code> without a type parameter:</p> <pre><code>var propertyinfo = typeof(T).GetProperty(sortExpressionStr); Type orderType = propertyinfo.PropertyType; var param = Expression.Parameter(typeof(T), "x"); var sortExpression = Expression.Lambda( Expression.Convert(Expression.Property(param, sortExpressionStr), orderType), param)); </code></pre> <p>This will create the proper <code>Func&lt;,&gt;</code> for you behind the scenes. If you want to compile the expression and use the delegate, you can only do this dynamically with</p> <pre><code>sortExpression.Compile().DynamicInvoke(param); </code></pre> <p>If you want to call the <code>OrderBy</code> extension method on <code>Queryable</code>, things get a little more complicated:</p> <pre><code>var propertyInfo = typeof(T).GetProperty(sortExpressionStr); Type orderType = propertyInfo.PropertyType; // first find the OrderBy method with no types specified MethodInfo method = typeof(Queryable).GetMethods() .Where(m =&gt; m.Name == "OrderBy" &amp;&amp; m.GetParameters().Length == 2) .Single(); // then make the right version by supplying the right types MethodInfo concreteMethod = method.MakeGenericMethod(typeof(T), orderType); var param = Expression.Parameter(typeof(T), "x"); // the key selector for the OrderBy method Expression orderBy = Expression.Lambda( Expression.Property(orderParam, propertyInfo), orderParam); // how to use: var sequence = new T[0].AsQueryable(); // sample IQueryable // because no types are known in advance, we need to call Invoke // through relection here IQueryable result = (IQueryable) concreteMethod.Invoke( null, // = static new object[] { sequence, orderBy }); </code></pre>
    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.
    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.
 

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