Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate an OrderBy Expression for LINQ/Lambda
    primarykey
    data
    text
    <p>I'm creating a proof of concept that uses Lambda/LINQ for dynamic where and orderby. The following code works for the where expression, but I can not figure out how to create an order by expression. For this example, if possible I would like to keep it simple; I would rather not write code that modifies an Expression Tree. </p> <pre><code>void Main() { DateTime productSince = DateTime.UtcNow.Subtract(new TimeSpan(1,30,0)); Expression&lt;Func&lt;Products, bool&gt;&gt; filter = d =&gt; d.CreatedDate &gt; productSince &amp;&amp; d.Price &lt; 100 ; List&lt;Products&gt; products = GetProducts(filter, Products); Console.WriteLine(products); } private static List&lt;Products&gt; GetProducts(Expression&lt;Func&lt;Products, bool&gt;&gt; filter, Table&lt;Products&gt; Products) { var products = Products.Where(filter); return products.ToList(); } </code></pre> <p>What I want is similar to the following but can not figure out the code to create the order by expression.</p> <pre><code>void Main() { DateTime productSince = DateTime.UtcNow.Subtract(new TimeSpan(1,30,0)); Expression&lt;Func&lt;Products, bool&gt;&gt; filter = d =&gt; d.CreatedDate &gt; productSince &amp;&amp; d.Price &lt; 100 ; Expression&lt;Func&lt;Products, ????&gt;&gt; orderBy = d =&gt; ??????; List&lt;Products&gt; products = GetProducts(filter, orderBy, Products); Console.WriteLine(products); } private static List&lt;Products&gt; GetProducts(Expression&lt;Func&lt;Products, bool&gt;&gt; filter, Expression&lt;Func&lt;Products, ???&gt;&gt; orderBy, Table&lt;Products&gt; Products) { var products = Products.Where(filter).OrderBy(orderBy); return products.ToList(); } </code></pre> <p>If you are wondering, I'm using LinqPad for this proof of concept.</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