1 回答
TA贡献1804条经验 获得超7个赞
让我们再看一些代码。我们将测试这两种情况:
首先,我们将使用两个 if 语句来测试 &&
我们将使用两个 Where 调用
所以:
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();
结果如下:
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
正如你所看到的,它是一样的。未通过 ConditionA 的元素不会针对 ConditionB 进行测试。
你可以在这里试试这个:https ://dotnetfiddle.net/vals2r
- 1 回答
- 0 关注
- 163 浏览
添加回答
举报
