[英]Group By and Count
I have retrieving data from DB which is something like that 我从DB中检索数据就是这样的
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
I need to return the result select item ID ,count the State grouped by item ID, state , type and comment 我需要返回结果选择项ID,计算按项ID,状态,类型和注释分组的状态
how can I do this using LINQ? 我怎么能用LINQ做到这一点?
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());
I am not sure what you mean but this I think this is what you want: 我不确定你的意思,但我觉得这就是你想要的:
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(..)
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()
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.