我有一个类似于此的T-SQL查询:
SELECT r_id, r_name, count(*)
FROM RoomBindings
GROUP BY r_id, r_name
我想使用LINQ做同样的事情。到目前为止,我到了这里:
var rooms = from roomBinding in DALManager.Context.RoomBindings
group roomBinding by roomBinding.R_ID into g
select new { ID = g.Key };
如何提取计数(*)和r_name部分?
答案 0 :(得分:67)
试试这个:
var rooms = from roomBinding in DALManager.Context.RoomBindings
group roomBinding by new
{
Id = roomBinding.R_ID,
Name = roomBinding.r_name
}
into g
select new
{
Id = g.Key.Id,
Name = g.Key.Name,
Count = g.Count()
};
由尼克编辑 - 添加方法链语法进行比较
var rooms = roomBinding.GroupBy(g => new { Id = g.R_ID, Name = g.r_name })
.Select(g => new
{
Id = g.Key.Id,
Name = g.Key.Name,
Count = g.Count()
});