[英]Linq return null on 'false' where clausule
我有一个类似的查询:
var solution = (from hit2 in Hits.Where(x => x.Combination.Count == 2)
where IsPossibleHit(hit2, 2, currentSymbols)
from hit3 in Hits.Where(x => x.Combination.Count == 3)
where IsPossibleHit(hit3, 3, currentSymbols)
from hit4 in Hits.Where(x => x.Combination.Count == 4)
where IsPossibleHit(hit4, 4, currentSymbols)
from hit5 in Hits.Where(x => x.Combination.Count == 5)
where IsPossibleHit(hit5, 5, currentSymbols)
select new
{
hitsList = new List<Hit>(){
hit2,
hit3,
hit4,
hit5}
}).ToList();
我的问题是,在进行分组时,如果可能命中hit2和hit3,则需要创建新对象,但是因为hit4返回false,整个组合都将被丢弃。
如何实现呢?
编辑:我想我没有弄清楚我需要什么,或者我的问题是:
我的问题是,当IsPossibleHit(hitN)返回false时,整个组合都将被丢弃(由linq),但是我需要的是无论如何都创建了对象,将返回false的命中设置为null,甚至不添加新对象的命中列表。
var solution = (from hit2 in Hits.Where(x => x.Combination.Count == 2)
where IsPossibleHit(hit2, 2, currentSymbols)
let h3 = from hit3 in Hits.Where(x => x.Combination.Count == 3)
where IsPossibleHit(hit3, 3, currentSymbols)
let h4 = from hit4 in Hits.Where(x => x.Combination.Count == 4)
where IsPossibleHit(hit4, 4, currentSymbols)
let h5 = from hit5 in Hits.Where(x => x.Combination.Count == 5)
where IsPossibleHit(hit5, 5, currentSymbols)
select new
{
hitsList = new List<Hit>(){
hit2,
h3,
h4,
h5}
}).ToList();
尝试类似的事情。 请检查语法,因为我尚未运行或编译它。
您想按匹配数分组,并仅在每个分组中保留可能的匹配数? 用Where
过滤,然后用GroupBy
过滤:
var groupedHits = from h in Hits
where h.Combination.Count >= 2 && h.Combination.Count <= 5
where IsPossibleHit(h, h.Combination.Count, currentSymbols)
group h by h.Combination.Count
我想您想做的是:
var res = Hits.Where(h => h.Combination.Count >= 2
&& h.Combination.Count <= 5
&& IsPossibleHit(h, h.Combination.Count, currentSymbols)
).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.