C# Linq在'false'上返回null,其中子句
我有一个类似的问题:C# Linq在'false'上返回null,其中子句,c#,.net,linq,C#,.net,Linq,我有一个类似的问题: 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 IsPossib
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,所以整个组合被丢弃
如何才能做到这一点
编辑:我想我没有弄清楚我需要什么,或者我的问题是什么:
我的问题是,当IsPossibleHithitN返回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 res = Hits.Where(h => h.Combination.Count >= 2
&& h.Combination.Count <= 5
&& IsPossibleHit(h, h.Combination.Count, currentSymbols)
).ToList();
如果你不想要hit4,那么为什么要把它放在where子句中呢?我只需要在IsPossibleHit for hit4返回true时才需要它。查询正在丢弃组合,而不是meThen,它正在执行您想要的操作,hit4返回false,整个组合将被丢弃。我可能误解了这里的意思。你想返回一个包含所有点击的列表,验证hit.composition.Count==n&&ispossiblehitt,n,currentSymbols,其中n在[2,3,4,5]中?@manji:这正是我想要做的。
var res = Hits.Where(h => h.Combination.Count >= 2
&& h.Combination.Count <= 5
&& IsPossibleHit(h, h.Combination.Count, currentSymbols)
).ToList();