简体   繁体   中英

Retrieve the two first elements that match a condition

I'm sure that this can be easily done with Linq but I can't figure it out.

var ls1 = plotter.Model.Series.FirstOrDefault(x => x.IsSelected);
var ls2 = plotter.Model.Series.FirstOrDefault((x => x.IsSelected)&&(ls2!=ls1));

What I'm pretending to do is to obtain the two first objects that have their property IsSelected set to true.

I can't use the syntax written above because the compiler can't use "local variable ls2 before it is declared".

使用“位置仅过滤所选结果,然后使用“ 获取”选择前两个,例如

plotter.Model.Series.Where(x => x.IsSelected).Take(2);

尝试这个:

var ls1and2 = plotter.Model.Series.Where(x => x.IsSelected).Take(2);
var ls1 = plotter.Model.Series.Where(x => x.IsSelected).Take(2);

您应该使用Take方法并执行此操作

var ls1 = plotter.Model.Series.Where(x => x.IsSelected).Take(2);

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