我有两个表,group
和group_user_xref
。
我需要用一列列出组,该列计算该组中有多少用户。目前,我的计数似乎只计算组,因为它总是返回 1。
到目前为止,我有:
(from g in db.group
join g2 in db.group_user_xref
on g.ID equals g2.group_uid
where g2.user_uid == user_auth_id
group g by new { g.ID, g.group_name, g.group_code, g.owner_id, g2.user_uid} into x
select new GroupSummary { ID = x.Key.ID, GroupOwner = x.Key.owner_id, GroupCode = x.Key.group_code, GroupName = x.Key.group_name, GroupCount = x.Count() }).ToList();
如何获取每个组中用户的计数?
您group by
添加到user id
中 - 因此它为每个组中的每个用户提供了一个分组 - 为此count
是一个
(from g in db.group
join g2 in db.group_user_xref
on g.ID equals g2.group_uid
where g2.user_uid == user_auth_id
group g by new
{
g.ID,
g.group_name,
g.group_code,
g.owner_id
} into x
select new GroupSummary
{
ID = x.Key.ID,
GroupOwner = x.Key.owner_id,
GroupCode = x.Key.group_code,
GroupName = x.Key.group_name,
GroupCount = x.Count()
}).ToList()