Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个名为 stacOverflows 的列表,其中包括一项,例如 IsOk、Number。
堆栈溢出
[0] -- IsOk = false; [0] -- Number = 5768; [1] -- IsOk = true; [1] -- Number = 4348;
如何在 LINQ 中获取 IsOk = true 的数值(如果有任何 IsOk = true)?
我应该使用any吗?
any
yourcollection.Where(i => i.IsOk).Select(i => i.Number).ToList()
如果您期望确切的一项,请使用Single:
Single
var item = collection.Single(i => i.IsOk).Number;
否则,使用Where/Select
Where
Select
var items = collection.Where(i => i.IsOk).Select(i => i.Number);