简单的Linq分组

时间:2011-09-07 09:32:38

标签: linq linq-to-sql

如何在Linq中使用group by

SqlServer示例

select 
    ActionGroupName,  
    sum(cast(isnull(solicit_count,0) as int)) as 'SolicitCount' 
from 
    [solicits]
group by
    ActionGroupName

我的linq版

var groupbyfilter = from v in lstSale
                    group v by v.ActionGroupName into g
                    select g.ActionGroupName, sum(g.solicit_count)

我没有以适当的方式得到它。

1 个答案:

答案 0 :(得分:5)

尝试:

var groupbyfilter = from v in lstSale
                    group v by v.ActionGroupName into g
                    select new 
                    { 
                      ActionGroupName = g.Key, 
                      Sum = g.Sum(x => x.solicit_count)
                    };