简体   繁体   中英

How can I get conditional data with LINQ

I have a list which is called stacOverflows which includes 1 item such as IsOk, Number.

StackOverflows

[0]   -- IsOk = false;
[0]   -- Number = 5768;
[1]   -- IsOk = true;
[1]   -- Number = 4348;

How can I get number value (if there is any IsOk = true) where IsOk = true with LINQ?

Should I use any ?

yourcollection.Where(i => i.IsOk).Select(i => i.Number).ToList()

If you expect exact one item, use Single :

var item = collection.Single(i => i.IsOk).Number;

Otherwise, use Where / Select

var items = collection.Where(i => i.IsOk).Select(i => i.Number);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM