Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq在'false'上返回null,其中子句_C#_.net_Linq - Fatal编程技术网

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();