7

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

3
  • 1
    You will never hit the last else if. If the last else if is going to be true then the very first if is true. Therefore you can't get to the last one. Commented Sep 30, 2009 at 12:40
  • I have now turned them into all if statements. Commented Sep 30, 2009 at 13:43
  • @ClareBear if they still have return statements in them then it won't help. You will never hit the last if statement that checks both conditions. You will still return from the first one only. I would also re-order them so that the check for personName && dateFrom not being null or empty occurs first. Commented Sep 30, 2009 at 13:49

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

I think I read because it is located within an if statement that it can't access anything outside of itself, not sure if that is true? As I am still getting the "The name 'a' does not exist in the current context error"
You're using >= instead of =>
2

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.

Comments

2

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.

Comments

1

..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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.