LINQ WHERE clause using if statements

I am using C # .net

I have two text fields that if! empty should be part of the WHERE clause in the LINQ query.

Here is my code

var result = from a in xxxx select a; if(!string.IsNullOrEmpty(personName)) { return result.Where(a >= a.forename.Contains(personName) || a.surname.Contains(personName) } else if(!string.IsNullOrEmpty(dateFrom)) { return result.Where(a >= a.appStartDateTime >= dateFrom.Date) } else if(!string.IsNullOrEmpty(personName) && !string.IsNullOrEmpty(dateFrom)) { return result.Where(a >= a.forename.Contains(personName) || a.surname.Contains(personName) && a.appStartDateTime >= dateFrom.Date); } 

I thought it would work, but I don't like it. Where and I can not access "a", for example, a.forename (the name "a" does not exist in the current context)

How am I mistaken, or is it really cannot be done?

Thanks in advance for your help.

Clare

+7
c # linq
source share
4 answers

Instead of this:

 result.Where(a.forename.Contains(personName)) 

Try the following:

 result.Where(a => a.forename.Contains(personName)) 

It seems that the Lambda operator (=>) is missing.

+6
source share

try it

 var result = from a in xxxx select a where (string.IsNullOrEmpty(personName) || a.forename.Contains(personName) || a.surname.Contains(personName)) && (string.IsNullOrEmpty(dateFrom) || a.appStartDateTime >= DateTime.Parse(dateFrom).Date); 

dateFrom looks like a string, so you need to parse it to get the date.

This logic should work, but I have not tested it. I could be wrong.

It seems to you that only if the name or surname of a person contains the name personName, if personName is not null or empty. Therefore, you can rewrite it to return things if personName is null or empty, or the name fore or sur contains the name of the person. Since || the short circuit operator does not check. Contains if name_name is empty or empty.

+2
source share

You can also combine predicates and make the logic shorter and easier to read:

 var result = from a in xxxx select a; if (!string.IsNullOrEmpty(personName)) result = result.Where(a => a.forename.Contains(personName) || a.surname.Contains(personName) if (!string.IsNullOrEmpty(dateFrom)) result = result.Where(a >= a.appStartDateTime >= dateFrom.Date) return result; 

This predicate combining method works well with AND conditions. If you need the conditions "OR", it is a bit more active. Fortunately, Joe Albahari created PredicateBuilder (as part of LINQKit ), which helps a lot with that.

Hope this helps.

+2
source share

.. or with one exit point:

 var result = from a in xxxx select a; Func<string, bool> func = null; if(!string.IsNullOrEmpty(personName)) { func = (a) => {a.forename.Contains(personName) || a.surname.Contains(personName)}; } else if(!string.IsNullOrEmpty(dateFrom)) { func = (a) => {a.appStartDateTime >= dateFrom.Date}; } else if(!string.IsNullOrEmpty(personName) && !string.IsNullOrEmpty(dateFrom)) { func = (a) => {a.forename.Contains(personName) || a.surname.Contains(personName) && a.appStartDateTime >= dateFrom.Date;}; } return result.Where(func); 
+1
source share

All Articles