Let's take a look at some more code. We'll test both scenarios:
- First we will test for && by using two if statements
- We'll use two Where calls
So:
var elements = new List<string>(new[] { "A", "B", "C" });
Console.WriteLine("C#'s &&");
elements.Where(x => {
if (x == "A")
{
Console.WriteLine("ConditionA is true");
if (1 == 1)
{
Console.WriteLine("ConditionB is true");
return true;
}
Console.WriteLine("ConditionB is false");
}
Console.WriteLine("ConditionA is false");
return false;
}).ToList();
Console.WriteLine();
Console.WriteLine("Double Linq.Where");
elements.Where(x => {
if (x == "A")
{
Console.WriteLine("ConditionA is true");
return true;
}
Console.WriteLine("ConditionA is false");
return false;
})
.Where(x => {
if (1 == 1)
{
Console.WriteLine("ConditionB is true");
return true;
}
Console.WriteLine("ConditionB is false");
return false;
}).ToList();
Here are the results:
C#'s &&
ConditionA is true
ConditionB is true
ConditionA is false
ConditionA is false
Double Linq.Where
ConditionA is true
ConditionB is true
ConditionA is false
ConditionA is false
As you can see, it's the same. Elements that don't pass ConditionA aren't tested for ConditionB.
You can try this out here: https://dotnetfiddle.net/vals2r