我有类似的查询:
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,甚至没有添加到新对象的命中列表中。
答案 0 :(得分:2)
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();
尝试类似的东西。请检查语法,因为我没有运行或编译它。
答案 1 :(得分:1)
您希望按点击次数进行分组,并且只保留每组中的可能点击次数?过滤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
答案 2 :(得分:1)
我认为你想要做的是:
var res = Hits.Where(h => h.Combination.Count >= 2
&& h.Combination.Count <= 5
&& IsPossibleHit(h, h.Combination.Count, currentSymbols)
).ToList();