2012-05-22 122 views
1

我已經檢索從DB數據是類似的東西集團通過和Count

ID - item ID - State - type - comment 
=== 
1 - 158  - 0 - 114589- AAAAA  
2 - 158  - 1 - 108965- BBBBB  
3 - 159  - 1 - 100145- CCCCC  
4 - 159  - 0 - 100145- DDDDD  
5 - 159  - 1 - 114589- EEEEE  
6 - 162  - 0 - 100145- FFFFF  
7 - 162  - 1 - 108965- GGGGG 

我需要返回的結果中選擇項目ID,計數按項目編號,狀態,類型進行分組國家和評論

我該如何使用LINQ來做到這一點?

回答

2
var grouped = from x in yourList 
      group by new {x.itemID, x.type ,x.comment }; 

var counts = grouped.Select(x=>new {key = x.Key, count = x.Count()); 
1

我不知道你的意思,但是這個我覺得這是你想要什麼:

var result = 
    from d in dataList 
    group d by new { d.State, d.Type, d.Comment } in g 
    select new { ItemId = g.Key, StateCount = g.Count(m => m.State) } 
    // I don't know what you mean with count the state. 
    // If you mean sum then you can use g.Sum(m => m.State) instead of g.Count(..) 
1
from DataRow row in dataSet 
group row by new 
{ 
    ID = (int) row["ID"], 
    State = row["State"].ToString(), 
    Type = (int)row["type"], 
    Comment = row["comment"].ToString() 
} into rowGroup 
select new 
{ 
    Key = rowGroup.Key, 
    Count = rowGroup.Count() 
};