一尘不染

Linq to SQL-分组依据和计数

sql

我正在尝试转换此查询(已经可以使用)

SELECT Building.NAME, COUNT([User].ID)
FROM BuildingUser
INNER JOIN Building ON Building.ID = BuildingUser.ID_BUILDING
INNER JOIN [User] ON [User].ID = BuildingUser.ID_USER
GROUP BY Building.NAME

对于Linq to SQL,但我不知道自己在做什么错。看我的尝试

from buildinguser in db.GetTable<BuildingUser>()
join building in db.GetTable<Building>()
on buildinguser.ID_BUILDING equals building.ID
join user in db.GetTable<User>()
on buildinguser.ID_USER equals user.ID
group building by building.NAME into grpBuilding
select new
{
    building = grpBuilding.Key,
    users = 
};

我只需要对建筑物进行分组并计算每个建筑物有多少用户。


阅读 167

收藏
2021-03-10

共1个答案

一尘不染

只需使用以下Count方法:

from buildinguser in db.GetTable<BuildingUser>()
join building in db.GetTable<Building>()
on buildinguser.ID_BUILDING equals building.ID
join user in db.GetTable<User>()
on buildinguser.ID_USER equals user.ID
group building by building.NAME into grpBuilding
select new
{
    building = grpBuilding.Key,
    users = grpBuilding.Count()
};
2021-03-10