-1

I am tring to put multple condition in where clause but not getting result as exprected.

what i am doing. in (LinqPad)

void Main()
{
    var lst = new List<Employee>()
    {
     new Employee() { Name="Name1", Address="address1",Desig="Desig1"}, 
     new Employee() { Name="Name2", Address="address2",Desig="Desig2"}, 
     new Employee() { Name="Name3", Address="address3",Desig="Desig3"}, 
     new Employee() { Name="Name4", Address="address4",Desig="Desig4"},
     new Employee() { Name="Name5", Address="address5",Desig="Desig5"},
     new Employee() { Name="Name6", Address="",Desig=""},
     new Employee() { Name="", Address="",Desig="Desig4"},
     new Employee() { Name="Name8", Address="address4",Desig=""}     
    };

var query =     (from d in lst
                select d).ToList();


var filter =query.Where(x=> !string.IsNullOrWhiteSpace(x.Name) && !string.IsNullOrWhiteSpace(x.Address)).ToList();              

filter.Dump();
}

// Define other methods and classes here
public class Employee
{
 public string Name {get;set;}
 public string Address {get;set;}
 public string Desig {get;set;}
}

Expected result is show the items where Name and Address are not null. but getting wrong result. which check whether Name or Address Is not null.

Result as 
    Name1   address1    Desig1
    Name2   address2    Desig2
    Name3   address3    Desig3
    Name4   address4    Desig4
    Name5   address5    Desig5
    Name8   address4    

Expected Result

    Name1   address1    Desig1
    Name2   address2    Desig2
    Name3   address3    Desig3
    Name4   address4    Desig4
    Name5   address5    Desig5
    Name6       
    Name8   address4    

It seems like Where condition only parse one condition at a time. thus both of these condition treeted as Or Condition.

JSJ
  • 5,653
  • 3
  • 25
  • 32
  • What result are you getting? Your code works as described for me. My result includes Names 1, 2, 3, 4, 5 and 8. (6 has an empty address, and 7 is missing the name, so they are excluded). – Dan Puzey Dec 06 '13 at 11:25
  • It will filter out the ones with empty name or adresses. – Ufuk Hacıoğulları Dec 06 '13 at 11:26
  • If you only want those that aren't null, you have to do `x.Name != null`. You're using `string.IsNullOrWhiteSpace`, which returns true if the string is either null, or only composed of whitespace. Null isn't the same thing as an empty string. Also, why are you doing `ToList()` *before* applying the filter? You're just wasting performance for no reason... – Luaan Dec 06 '13 at 11:28
  • I can't understand what is your problem. I executed your code and I got all the Employees with not null or whitespace Name and Address – Christos Dec 06 '13 at 11:30
  • 1
    I'm assuming that the OP is conflating the terms "null" and "empty" since the test data only includes empty values (and no nulls). – Dan Puzey Dec 06 '13 at 11:30
  • can you describe what is wrong with result you get? – Guru Stron Dec 06 '13 at 11:34
  • Can you give a sample of the expected output? The code posted works fine for me. – DGibbs Dec 06 '13 at 11:37
  • @Luaan `string.IsNullOrWhiteSpace("")` will return true. Try it.. – DGibbs Dec 06 '13 at 11:43
  • @Guru. see the updated result section. . – JSJ Dec 06 '13 at 11:49
  • 1
    @Luaan In addition, see the [MSDN for IsNullOrWhiteSpace](http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace(v=vs.110).aspx) - `Indicates whether a specified string is null, empty, or consists only of white-space characters.`. It is an extension of `IsNullOrEmpty` to include whitespace checking. – DGibbs Dec 06 '13 at 11:53
  • 1
    @DGibbs: Yeah, I assumed that "All whitespace" obviously includes empty strings, but you're right, I should have been clearer :)) The whole misunderstanding is thanks to the fact that apparently, the OP asked a completely wrong question - he wanted an OR, while we answered how to improve his AND. I didn't expect his problem is that he can't do boolean logic properly :D – Luaan Dec 06 '13 at 12:15
  • 1
    @Luaan Yep, that's why it's always a good idea to include sample input and output data, it would have been spotted much sooner! – DGibbs Dec 06 '13 at 12:20

1 Answers1

2

Ok, after your edit your question is now clear.

The problem is that you are using a conditional AND && in your query. If the first operand is false, then the second isn't evaluated because the result is always false since both operands must evaluate to true.

What you are doing is checking for null/empty/whitespace on both address and name.

Which is why the record:

  new Employee() { Name="Name6", Address="",Desig=""},

Is not appearing in your expected output. Name is not null/empty but Address is so the statement returns false and it is not included.

You query should be:

var filter = query.Where(x => !string.IsNullOrWhiteSpace(x.Name)
             || !string.IsNullOrWhiteSpace(x.Address)).ToList();  

Conditional OR will ensure that if either one of the two operands evaluates to true, then the whole statement is true.

If the first operand is true, then the second isn't evaluated a.k.a "short circuit evaluation".

DGibbs
  • 14,316
  • 7
  • 44
  • 83
  • or either i can use someting like query.Where(x => !(string.IsNullOrWhiteSpace(x.Name) && string.IsNullOrWhiteSpace(x.Address))).ToList(); – JSJ Dec 06 '13 at 12:47
  • @JSJ That will not have the same effect as `||` though. Your query will return true only if the first operand is null/empty and the second is not. The `||` OR will check that at least one of the operands is not null/empty. – DGibbs Dec 06 '13 at 12:50