How to use when condition

can we be dynamically added when the condition on the linq request?

eg:

class Result { string v1; string v2; string v3; } List<Result> result = (from r in results select r); //i want to do something like the following.... if(conditionA) { result = result appened (or v1 = xxx) } else if(conditionB) { result = result appened (or v2 = xxx) } else if(conditionC) { result = result appened (or v3 == xxx) } 

Does anyone know how to handle a condition in Linq ????

Thurs

+4
source share
7 answers

With and relations of sentences, you can simply simply add the .Where() filter method, as such:

 where conditionOriginal(r) and conditionDynamic(r) 

a

 var results = (from r in originalResults where originalConditions(r) select r); ... if (conditionA) results = results.Where(r => conditionDynamic(r)); 

To add an or relationship, you must combine with the original result set, for example:

 where conditionOriginal(r) or conditionDynamic(r) 

becomes

 var results = (from r in originalResults where conditionOriginal(r) select r); ... if (conditionB) results = results.Union((from r in originalResults where conditionDynamic(r) select r)); 

or

 if (conditionB) results = results.Union(originalResults.Where(conditionDynamic(r))); 
+1
source

If you want to build it dynamically, you can use PredicateBuilder

+3
source

Just add the Where query statement to your query:

 if(conditionA) { result = result.Where(r => r.v1 == xxx); } else if(conditionB) { result = result.Where(r => r.v2 == xxx); } else if(conditionC) { result = result.Where(r => r.v3 == xxx); } 

Note that your results variable should be declared as IEnumerable<Result> , not List<Result>

+1
source

You can do it:

 if (conditionA) { result = result.Where(p => p.v1 == xxx); // Just guessing at the intent here. } // Repeat as necessary... 

Or that:

 if (conditionA) { result = from r in result where p.v1 == xxx select r; } 
+1
source

Since Linq delays execution, you can simply add where to your request and call the list at the end to execute:

 var query = from r in results; //i want to do something like the following.... if(conditionA) { query = result.Where(x => x.v1 = xxx); } else if(conditionB) { query = result.Where(x => x.v2 = xxx); } else if(conditionC) { query = result.Where(x => x.v1 = xxx); } List<Result> result = query.ToList(); 
0
source

Other answers are the easiest solution. If you want to execute one execution, you can also use expression trees:

http://blogs.msdn.com/abhinaba/archive/2006/03/01/541179.aspx http://www.devsource.com/c/a/Languages/Understanding-LINQ-Expression-Trees/1/

0
source

Well, you can always call a function in the where clause and create your condition there:

 ... public bool MeetsConditions(Result r, bool a, bool b) { bool result = false; if(a) result = result || r.v1==xxx if(b) result = result && r.v2==xxx return result; } ... var q = select from r in results where MeetsConditions(r, conditionA, conditionB) 
0
source

Source: https://habr.com/ru/post/1302307/


All Articles