将SQL转换为LINQ

时间:2014-05-22 17:58:47

标签: sql linq

如何将以下SQL语句转换为C#中的LINQ语句?

select BLDG_CODE, count(ROOM) "Number of Rooms"
from LOCATION
group by BLDG_CODE;

非常感谢

2 个答案:

答案 0 :(得分:1)

var query = from l in Locations
            group l by l.BLDG_CODE into g
            select new { 
                BLGD_CODE = g.Key,
                NumberOfRooms = g.Count()
            };

答案 1 :(得分:1)

或者,如果您更喜欢方法语法......

var query = Locations.GroupBy(location => location.BLDG_CODE)
                     .Select(group => new {  
                                            BuildingCode = group.Key,
                                            NumberOfRooms = group.Count()
                                          });