LINQ WHERE clause using if statements

.net, c#, linq

Solution

Instead of this:

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

Try this:

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

You appear to be missing the Lambda operator (=>).

Problem

I am using c#.net I have two textboxes which if !empty need to be part of a WHERE clause within a 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 this would work but it doesn't like the .Where and I cant access the 'a' for example a.forename (The name 'a' does not exist in the current context) What am I going wrong, or can this not actually be done? Thanks in advance for any help. Clare

Original source