3

如何将其转换为 linq?

select i.CatName,COUNT(*) as n_items
from ixcxs i
group by i.CatName

或 lambda 表达式会有所帮助

4

2 回答 2

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() });
于 2012-12-14T00:31:12.677 回答
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()
}
于 2012-12-14T00:10:26.470 回答