c# - LINQ, Large filter on a query -


I am creating a section of an application that swings to draw information about the transaction from the database, the nature of the data Due to this, there are several columns in the table that I want to filter. I have a filter selection box with 15 fields, which I want to be able to create a section for the LINQ statement. The interesting part comes when I want to clear some areas. For example I want to be able to filter any or all of the:

  • Transaction type
  • Response code
  • Transaction Amount
  • Too much

I can make a predicate that looks

  function & lt; Transactions, bulls & gt; Pred = t = & gt; T. Responsecode == ResponseCode & amp; T.TransactionType == Transaction Type & amp; Amp; T. Transaction amount & gt; 100.00;  

But to be able to decide which areas I am getting together:

  Func < Transactions, bool & gt; Pred = t = & gt; truth; If (ResponseCode! = Null) pred.AndAlso (t = & gt; ResponseCode == ResponseCode); // Rinse and repeat  

and then to understand the point where the section of the LINQ statement.

This is exactly the way I want it but it's complicated. Are there other ways to do this?

UPDATE: Thanks for the comments. I'm not using SQL from LINQ, I'm using LINQ on a collection of objects from a collection. How do you create application filters?

  • In dynamic SQL ... where you only have one section - Add predicates to you with needed.
  • In building linq query ... as many as you want, where clauses get

example:

  IQueryable & Lt; Transactions & gt; Query = db transaction; If (filterByTransactionType) {query = query.Where (T => T. transyase == theTransactionType); } If (filterByResponseCode) {query = query.Where (T = & gt; T. Response code == theResponseCode); } If (filterByAmount) {query = query.Where (T = & gt; T. Transactions Amount & gt; Amount); }  

Another example:

  list & lt; Expression & lt; Funk & lt; Transactions, bool & gt; & Gt; & Gt; Filter = GetFilterExpressions (); IQueryable & LT; Transactions & gt; Query = db transaction; Filter. FOREX (F = & gt; query = query. WHERE (f));  

Comments