这两个语句中哪个更快/更好?
myList.Where(x =>
{
bool itemOne= x.ItemOne == paramItemOne;
bool itemTwo = x.ItemTwo == paramItemTwo;
return itemOne && itemTwo;
})
myList.Where(x => x.ItemOne == paramItemOne).Where(x=>x.ItemTwo == paramItemTwo)
还是它们相同?
最佳答案
我也不会说。最快的将是:
myList.Where(x => x.ItemOne == paramItemOne && x.ItemTwo == paramItemTwo)
(假设编译器/JIT 没有优化掉第一种形式中的变量赋值)
第二种形式会更慢,因为它可能涉及对提供给 Where 语句的委托(delegate)的明显更多的方法调用。
关于c# - Linq 方法正文最佳实践问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2429084/