简体   繁体   中英

LINQ WHERE clause using if statements

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

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 (=>).

try this


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 appears to be a string so you have to parse it to get a date time.

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

You seem to only care if the forename or surname contain personName if the personName is not null or empty. So you can rewrite it to return things if the personName is null or empty or the fore or sur name contains person name. Since the || operator is short circuiting it will not check Contains if the personName is null or empty.

You can also combine the 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 method of combining predicates works well with 'AND' conditions. If you need to 'OR' conditions, it's a little bit more involved. Thankfully, Joe Albahari has created PredicateBuilder (as part of LINQKit ) that helps with this greatly.

Hope this helps.

..or with just 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM