3

It is possible to add or to linq query after declaration?

I mean if i have :

Query.Where(f => some-condition ); 

I wand to add "OR" after that:

if (some-condition) 
   Query.WhereOr(f => some-condition ); 
yantrab
  • 2,482
  • 4
  • 31
  • 52
  • 3
    It's C# code. How do you usually implemtn an OR operator in C#? This is the same: `Where(f => some-condition || some-other-condition)`. Don't try to overcomplicate things. – jmcilhinney Nov 21 '17 at 07:43
  • 1
    `Query.Where(f => some-condition || some-condition2);`? – Dmitry Bychenko Nov 21 '17 at 07:44
  • I think, before adding that condition, there was an if statement first? @jmcilhinney – Willy David Jr Nov 21 '17 at 07:45
  • 1
    If you keep the `Query` around (without `Where` applied) it might be possible to work with something like `Query.Where(cond1).Union(Query.Where(cond2))` but I don't know whether the result will be any similar to a simple `||` – grek40 Nov 21 '17 at 07:46
  • 2
    http://www.albahari.com/nutshell/predicatebuilder.aspx – Drag and Drop Nov 21 '17 at 08:32

3 Answers3

1

Looks like you could be looking for a Predicate builder Or you can just stack some .Where clause as linq is lazy.

As your question lacks of context, imagine querying a Person table based on a form (2 fields/1 checkbox).

// Where Stacking
if (!string.IsNullOrEmpty(FieldA.Text))
{// OR
    result = query.Where(item => item.Something.Contains(FieldA.Text));
}
if (!string.IsNullOrEmpty(FieldB.Text))
{// AND
    result = result.Where(item => item.SomethingElse.Contains(FieldB.Text));
}

// Predicate
if (CheckBox1.Checked || CheckBox2.Checked || CheckBox3.Checked))
{//Here your initialise your predicate to false because you are going to do OR
    var predicate = PredicateBuilder.False<SearchItem>();
    if(CheckBox1.Checked) predicate = predicate.Or(item => item.Foo == 1);
    if(CheckBox2.Checked) predicate = predicate.Or(item => item.Foo == 2);
    if(CheckBox3.Checked) predicate = predicate.Or(item => item.Foo == 3);
    result = result.Where(predicate);
}

Stacking Where is a good solution but can produce a little overheat but nothing that is really noticable.

PS: Article of Jon about nesting where https://codeblog.jonskeet.uk/2011/06/16/linq-to-objects-and-the-performance-of-nested-quot-where-quot-calls/

Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
  • 2
    Wouldn't stacking `.Where` clauses behave as `and` instead of `or` ? – Rafalon Nov 21 '17 at 09:26
  • @Rafalon, it's not the stacking that cause AND, OR but the `**query** = **query**.Where` – Drag and Drop Nov 21 '17 at 09:38
  • Then what do you mean by stacking ? I tried `integers.Where(x => x > 3).Where(x => x < 5)` where `integers == {1,2,3,4,5,6}` and it returns only 4 as if it was `integers.Where(x => x > 3 && x < 5)` – Rafalon Nov 21 '17 at 09:40
  • @Rafalon, I have edit my thought into code. But your are right and this way can behave as a sub query instead of a or close depending on a lot of things. – Drag and Drop Nov 21 '17 at 09:46
0

You should use LinqKit.PredicateBuilder class

Example:

var predicate = PredicateBuilder.True<Product>();
if(inputPrice > 100)
   predicate = predicate.And (p => p.Price > 100);
if(inputName == "S%")
   predicate = predicate.And (p => p.Name.StartsWith("S"));

Finally,

var result = products.Where(predicate);

More details Wiki

Sunil
  • 3,404
  • 10
  • 23
  • 31
0

IEnumerable and IQueryable to some extent will allow you to chain conditions

Query.Where(f=> some-condition || some_other_condition);
// or
var q = Query.Where(f=> some-condition)
if(stuff_needs_to_happen)
  q = q.Where(f => stuff_that_needs_to_happen)

The reason for the "to some extent" is that with IQueryable the underlying query language may not support some of the conditions in the predicate (custom functions and so on).

reckface
  • 5,678
  • 4
  • 36
  • 62
  • I believe the second part is equivalent to `Query.Where(f => some-condition && stuff_that_needs_to_happen)` and not to `Query.Where(f => some-condition || stuff_that_needs_to_happen)` – Rafalon Nov 21 '17 at 09:18