c# - Linq PredicateBuilder with conditional AND, OR and NOT filters -


We have a project for SQL from SQL, for which I have to re-add some search pages to select the client Whether they wish to search and or or .

However I know about resuming LINQ queries and I like this work very well. I'm effectively a class with my predecessors, e.g.:

  Internal steady expression & lt; Funk & lt; Job, bool & gt; & Gt; Description (string word) {return p = & gt; P.Description.Contains (words); }  

I'm doing this to search (briefly dropped some code):

  Public expression & lt; Funk & lt; Jobs, bool & gt; & Gt; ToLinqExpression () {var predicates = new list & lt; Expression & lt; Func & lt; Jobs, bool & gt; & Gt; & Gt; (); // build-up here predicts that if (SearchType == SearchType.And) {query = Predicatebuilder.True & lt; Job & gt; (); } And {query = PredicateBuilder.False & lt; Jobs & gt; (); } Foreach (predicates in var predicate) {if (SearchType == SearchType.And) {query = query.And (predicate); } Else {query = query.Or (predicate); }} Return query; }  

Although I am very happy with this, I have two concerns:

  1. If / Other blocks that evaluate search type assets, Like they can make a potential code odor.
  2. The client is now insisting on being able to 'no more' / 'or do not search'.

To know the point 2, I think I can do this by just writing my expressions again, for example:

  Internal steady expression & lt; Funk & lt; Job, bool & gt; & Gt; Description (string word, bool inverse) {if (invert) {return p = & gt; ! P.Description.Contains (words); } And {return p = & gt; P.Description.Contains (words); }}  

Though it feels like a cooled, which usually means that there is a better solution. Can someone recommend how this can be improved? I know about dynamic LINQ, but I really do not want to lose LINQ's strong typing.

If you are looking for fewer rows then you can replace the other with the turner operator Are:

  query = SearchType == SearchType.And? PredicateBuilder.True & lt; Job & gt; (): PredicateBuilder.False & lt; Job & gt; (); Forex (forecasts in forecasts) {query = SearchType == SearchType.And? Query.And (predicate): query.Or (predicate); }  

for and not / or 'no' part ! The operator should do the trick.

PD: Do you have to set the exam for the foreach section correctly, as far as I remember you create that expression That's time later, so you can have a lexical reference to estimate the last set in the last round, and so you have to use the temporary variable to save the value of each instance.

Edit: / Strong> If you want to deny an expression programmatic, then it is a difficult one, you can try to do something like this:

  Internal Stable Expression & lt; Func & lt; Job, bool & gt; & Gt; Details (String term, Bull invoices) {Return Negate XP & lt; Funk & lt; Job, bull & gt; & Gt; (P = & gt; p.Description.Contains (term), invert); }  

And the Negatex method would be something like this:

  Public Static Expression & lt; TDelegate & gt; NegateExp & lt; TDelegate & gt; (Expression & lt; TDelegate & gt; Expression, Bool Inverse) {If (Inverted) {Return expression. Lambda & lt; TDelegate & gt; (Expression. No (expression. Body), expression. Parameter); } Return expression; }  

You can take a look at this question for more examples


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -