如何将此转换为linq?
select i.CatName,COUNT(*) as n_items
from ixcxs i
group by i.CatName
或lambda表达将有帮助
答案 0 :(得分:4)
您无需从组中选择第一项来获取CatName - 而是使用分组键:
var query = from i in ixcxs
group i by i.CatName into g
select new {
CatName = g.Key,
n_items = g.Count()
};
与方法语法相同:
var query = ixcxs.GroupBy(i => i.CatName)
.Select(g => new { CatName = g.Key, n_items = g.Count() });
答案 1 :(得分:2)
尝试
ixcxs.GroupBy(g => g.CatName)
.Select(s => new
{
CatName = s.Key,
n_items = s.Count()
});
查询语法中的相同内容:
from i in ixcxs
group i by i.CatName into g
select new
{
CatName = g.Key,
n_items = g.Count()
}