Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple conditions in Where statement

Status
Not open for further replies.

claws2rip

Programmer
Dec 14, 2001
80
US
How do I write a where statement that will exclude records if both conditions are met.

example:

var filteredRecords=AllRecords.Where(w=>w.Amount!=0 && w.Sales!=0);

so if any record had amount=0 or sales=0 I want to include.
I only want to exclude records where BOTH sales and amount are equal to 0.

 
found a solution

var filteredRecords=AllRecords.Where(w=> !(w.Amount==0 && w.Sales==0));

 
thats the same as writing

AllRecords.Where(w => w.Amount != 0 || w.Sales != 0);

You can distribute the negation, or pull it out.

!(Condition == x || Condition == y). When 'multiplying' the inner conditions by the !, you switch OR to AND or AND to OR, and Equals to Not Equals or Not Equals to Equals.

so... !(Condition == X || Condition ==y) .... Condition != X && Condition != Y is equivalent.
!(Condition != X && Condition == y) is equivalent to Condition == X || Condition != Y

Sound like you just didn't know about the ||, OR, statement, but still useful to know this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top