C# Linq to SQL:如何对子选择执行计数

C# Linq to SQL:如何对子选择执行计数,c#,sql,linq,linq-to-sql,C#,Sql,Linq,Linq To Sql,我仍在努力了解如何正确使用LINQ to SQL,而不仅仅是编写自己的存储过程 在代码中,一个userId被传递到方法中,然后LINQ使用它从GroupTable表中获取与该userId匹配的所有行。GroupUser表的主键是GroupUserId,它是组表中的外键 这很好,但我还想返回组中的用户总数以及属于组所有权的联系人总数 伪代码啊 我在SQL中编写的LINQ版本似乎起到了作用,我没想到它会起作用 /// <summary> /// Return summar

我仍在努力了解如何正确使用LINQ to SQL,而不仅仅是编写自己的存储过程

在代码中,一个userId被传递到方法中,然后LINQ使用它从GroupTable表中获取与该userId匹配的所有行。GroupUser表的主键是GroupUserId,它是组表中的外键

这很好,但我还想返回组中的用户总数以及属于组所有权的联系人总数

伪代码啊


我在SQL中编写的LINQ版本似乎起到了作用,我没想到它会起作用

    /// <summary>
    /// Return summary details about the groups a user belongs to
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public static List<Group> GroupsForUser(int userId)
    {
        DataAccess.KINv2DataContext db = new DataAccess.KINv2DataContext();
        List<Group> groups = new List<Group>();

        groups = (from g in db.Groups
                  join gu in db.GroupUsers on g.GroupId equals gu.GroupId
                  where g.Active == true && gu.UserId == userId
                  select new Group
                  {
                      GroupId = g.GroupId,
                      Name = g.Name,
                      CreatedOn = g.CreatedOn,
                      ContactCount = (from c in db.Contacts where c.OwnerGroupId == g.GroupId select c).Count(),
                      MemberCount = (from guu in db.GroupUsers where guu.GroupId == g.GroupId 
                                     join u in db.Users on guu.UserId equals u.UserId
                                     where u.Active == true 
                                     select gu ).Count()
                  }).ToList<Group>();

        return groups;
    }

我在SQL中编写的LINQ版本似乎起到了作用,我没想到它会起作用

    /// <summary>
    /// Return summary details about the groups a user belongs to
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public static List<Group> GroupsForUser(int userId)
    {
        DataAccess.KINv2DataContext db = new DataAccess.KINv2DataContext();
        List<Group> groups = new List<Group>();

        groups = (from g in db.Groups
                  join gu in db.GroupUsers on g.GroupId equals gu.GroupId
                  where g.Active == true && gu.UserId == userId
                  select new Group
                  {
                      GroupId = g.GroupId,
                      Name = g.Name,
                      CreatedOn = g.CreatedOn,
                      ContactCount = (from c in db.Contacts where c.OwnerGroupId == g.GroupId select c).Count(),
                      MemberCount = (from guu in db.GroupUsers where guu.GroupId == g.GroupId 
                                     join u in db.Users on guu.UserId equals u.UserId
                                     where u.Active == true 
                                     select gu ).Count()
                  }).ToList<Group>();

        return groups;
    }
    /// <summary>
    /// Return summary details about the groups a user belongs to
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public static List<Group> GroupsForUser(int userId)
    {
        DataAccess.KINv2DataContext db = new DataAccess.KINv2DataContext();
        List<Group> groups = new List<Group>();

        groups = (from g in db.Groups
                  join gu in db.GroupUsers on g.GroupId equals gu.GroupId
                  where g.Active == true && gu.UserId == userId
                  select new Group
                  {
                      GroupId = g.GroupId,
                      Name = g.Name,
                      CreatedOn = g.CreatedOn,
                      ContactCount = (from c in db.Contacts where c.OwnerGroupId == g.GroupId select c).Count(),
                      MemberCount = (from guu in db.GroupUsers where guu.GroupId == g.GroupId 
                                     join u in db.Users on guu.UserId equals u.UserId
                                     where u.Active == true 
                                     select gu ).Count()
                  }).ToList<Group>();

        return groups;
    }