2011-06-15 49 views
-1

我有一个类似的查询:LINQ的返回NULL“假”,其中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,甚至不会将其添加到新对象的Hit列表中。

+0

如果你不想hit4,那么为什么把它放在where子句中? – 2011-06-15 13:56:32

+0

只有当hit4的IsPossibleHit返回true时,我才需要它。 (查询是放弃组合,而不是我) – 2011-06-15 13:58:07

+0

然后,它正在做你想做的,hit4返回false,整个组合被丢弃。我可能会误解这里。 – 2011-06-15 13:59:25

回答

1

我想你想要做的是:

var res = Hits.Where(h => h.Combination.Count >= 2 
         && h.Combination.Count <= 5 
         && IsPossibleHit(h, h.Combination.Count, currentSymbols) 
       ).ToList(); 
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

您想按照点击次数进行分组并仅保留每个组中的可能匹配数?与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