获取记录计数并加入实体框架中的另一个实体

时间:2011-04-05 08:42:18

标签: entity-framework

我有两张桌子:

1:角色

2:UserRoles

请考虑以下命令:

在SQL中:

SELECT     RoleID, RoleName,
                          (SELECT     COUNT(UserID) AS UserCount
                            FROM          dbo.UserRoles AS UR
                            WHERE      (RoleID = R.RoleID)) AS UserCount
FROM         dbo.Roles AS R

实体框架中的相同脚本是什么?

1 个答案:

答案 0 :(得分:2)

这是LINQ to Entities查询,例如:

var q = from role in context.Roles
  select new {
    role.RoleID,
    role.RoleName,
    UserCount = role.UserRoles.Count()
  };