Note that there are some explanatory texts on larger screens.

plurals
  1. POLINQ, Large filter on a query
    primarykey
    data
    text
    <p>I am building a section of an application that revolves around pulling information about transactions out of the database. Due to the nature of the data, there are many columns in the table that I want to filter on. I have a filter selection box with 15 fields that I want to be able to build up a where clause for the LINQ statement. The interesting part comes when I want certain fields to be null. For example I want to be able to filter on any or all of:</p> <ul> <li>Transaction Type</li> <li>Response Code</li> <li>Transaction Amount</li> <li>Many more</li> </ul> <p>I can build up a predicate that looks like</p> <pre><code>Func&lt;Transaction, bool&gt; pred = t =&gt; t.ResponseCode == ResponseCode &amp;&amp; t.TransactionType == TransactionType &amp;&amp; t.TransactionAmount &gt; 100.00; </code></pre> <p>But in order to be able to choose which fields to include in the predicate I am <a href="https://stackoverflow.com/questions/491780/concatenating-lambda-functions-in-c">concatenating the predicates</a> together:</p> <pre><code>Func&lt;Transaction, bool&gt; pred = t =&gt; true; if(ResponseCode != null) pred.AndAlso(t =&gt; t.ResponseCode == ResponseCode); // Rinse and repeat </code></pre> <p>And then passing that predicate to the where clause of the LINQ statement.</p> <p>This works exactly the way I want it, but is rather complicated. Are there any other ways of doing this?</p> <p>UPDATE: Thanks Justice for the comments. I'm not using LINQ to SQL, I'm using LINQ on a collection of objects from a repository. How would you programatically build an Expression filter?</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.
 

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