2012-03-06 184 views
14

我無法用LINQ Query解決這個問題。LINQ嵌套組

所以我們的表結構如下: Id || bug_category || bug_name || bug_details || bug_priority

我想先通過bug_category進行分組。對於每個bug_category,我想依次按bug_priority進行分組。

所以基本上我想是這樣的:

bug_category =音頻::沒有錯誤的 - >臨界值= 3,中等= 2,低= 7級的錯誤。
bug_category = VIDEO :: BUGS的數量 - > Critical = 5,Medium = 1和Low = 9個錯誤。

的下面查詢返回類別和customer_priority的所有獨特組合:

(其中RawDataList僅僅是具有上述結構的數據的列表)

 var ProceesedData = from d in RawDataList 
         group d by new { d.bug_category, d.bug_priority } into g 
         select new 
         { 
          g.Key.bug_category, 
          g.Key.bug_priority 
         }; 

的下面查詢返回類別,然後是該類別中的記錄列表:

  var ProceesedData = from d in RawDataList 
         group d by d.bug_category into g 
         select new { g.Key, records = g 
         }; 

但是我是聯合國能夠繼續進行,因爲ProcessedData(返回變量)是一個未知類型。對此有何想法?

回答

25

你想,我懷疑(名稱改爲更地道):

var query = from bug in RawListData 
      group bug by new { bug.Category, bug.Priority } into grouped 
      select new { 
       Category = grouped.Key.Category, 
       Priority = grouped.Key.Priority, 
       Count = grouped.Count() 
      }; 

然後:

foreach (var result in query) 
{ 
    Console.WriteLine("{0} - {1} - {2}", 
         result.Category, result.Priority, result.Count); 
} 

或者(但見後):

var query = from bug in RawListData 
      group bug by new bug.Category into grouped 
      select new { 
       Category = grouped.Category, 
       Counts = from bug in grouped 
         group bug by grouped.Priority into g2 
         select new { Priority = g2.Key, Count = g2.Count() } 
      }; 

foreach (var result in query) 
{ 
    Console.WriteLine("{0}: ", result.Category); 
    foreach (var subresult in result.Counts) 
    { 
     Console.WriteLine(" {0}: {1}", subresult.Priority, subresult.Count); 
    } 
} 

編輯:由於在評論中指出,這將導致多個SQL查詢。爲了獲得類似的結果,但結構更有效,你可以使用:

var dbQuery = from bug in RawListData 
       group bug by new { bug.Category, bug.Priority } into grouped 
       select new { 
        Category = grouped.Key.Category, 
        Priority = grouped.Key.Priority, 
        Count = grouped.Count() 
       }; 

var query = dbQuery.ToLookup(result => result.Category, 
          result => new { result.Priority, result.Count }; 


foreach (var result in query) 
{ 
    Console.WriteLine("{0}: ", result.Key); 
    foreach (var subresult in result) 
    { 
     Console.WriteLine(" {0}: {1}", subresult.Priority, subresult.Count); 
    } 
} 
+2

第二個查詢會爲每個組創建輔助查詢,所以我想一個更好的解決方案是第一個,接着按類別查看 – 2012-03-06 06:34:52

+0

@AdrianIftode:你試過了嗎?我沒有,但我會*期望*生成適當的SQL來一次完成這一切。 – 2012-03-06 06:58:33

+0

是的,與LinqPad,一個查詢的類別組和其他X優先級組的查詢 – 2012-03-06 07:02:31

5

我覺得你這樣的搜索的東西:

var processedData = 
     rawData.GroupBy(bugs => bugs.bug_category, 
      (category, elements) => 
      new 
       { 
        Category = category, 
        Bugs = elements.GroupBy(bugs => bugs.bug_priority, 
             (priority, realbugs) => 
             new 
              { 
               Priority = priority, 
               Count = realbugs.Count() 
              }) 
       }); 
    foreach (var data in processedData) 
    { 
     Console.WriteLine(data.Category); 

     foreach (var element in data.Bugs) 
      Console.WriteLine(" " + element.Priority + " = " + element.Count); 
    } 
+0

感謝您提供解決方案。欣賞它! – Nandu 2012-03-06 19:13:07

+0

@Nandu:你知道這與我的第二個查詢基本相同,只是在lambda表達式中,對吧?請注意,它具有與Adrian指出的相同的糟糕表現(因爲它是相同的查詢)。 – 2012-03-06 19:50:57

+2

比Jon Skeet寫出你的答案要花費9分鐘的危險;) – 2015-04-15 01:23:52

12

這是實現嵌套的分組更簡單的方法。我已經在內存集合中對它進行了測試,無論您的特定數據庫提供程序是否可以處理好它可能會有所不同,或者它的性能表現如何都不得而知。

假設你有兩個屬性,且由國家和國家想組:

var grouped = People 
    .GroupBy(l => new { l.State, l.Country})//group by two things 
    .GroupBy(l=> l.Key.Country)//this will become the outer grouping 


foreach(var country in grouped) 
{ 
    foreach(var state in country) 
    { 
    foreach(var personInState in state) 
    { 
     string description = $"Name: {personInState.Name}, State: {state.StateCode}, Country: {country.CountryCode}"; 
     ... 

    } 
    } 
} 
+0

這是一個難以找到的答案! – 2015-12-15 17:41:20

+0

是的,當我需要這些東西時,我花了一段時間才弄清楚這一點,而當我需要這樣做時,我實際上回到了自己的答案。 – AaronLS 2016-11-28 00:24:02