2011-06-15 66 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