gpt4 book ai didi

c# - 在 lambda Where() 中全选

转载 作者:行者123 更新时间:2023-11-30 19:10:40 25 4
gpt4 key购买 nike

我知道这行得通:

var result = someCollection.Select(x=>x);

我正在使用 Where() 方法获得类似的结果:

var result = someCollection.Where(x=> someBool ? x : x.Key == 1);

如果 someBool 为真,我想选择“一切”。上面的代码不起作用。有什么方法可以使用 lambda 解决这个问题吗?

最佳答案

使用|| 运算符,如果someBool 为真,它将选择所有记录。

var result = someCollection.Where(x=> someBool || x.Key == 1);

关于c# - 在 lambda Where() 中全选,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16435425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com