2012-05-22 2 views
1

내가 항목 ID, 상태, 종류에 따라 상태를 계산하고그룹에 의해 카운트

을 언급, 결과 선택 항목 ID를 반환 할 필요가

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 

같은 것입니다 DB에서 데이터를 검색 한

어떻게 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() 
};