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

时间:2010-03-16 11:14:07

标签: c# sql linq linq-to-sql

我仍然试图正确理解如何正确使用LINQ-to-SQL,而不仅仅是编写自己的sprocs。

在代码属于userId传递给方法,然后LINQ使用它来获取 GroupTable 表中与 userId 匹配的所有行。 GroupUser 表的主键是 GroupUserId ,它是表中的外键。

    /// <summary>
    /// Return summary details about the groups a user belongs to
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public 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
                  {
                      Name = g.Name,
                      CreatedOn = g.CreatedOn
                  }).ToList<Group>();


        return groups;
    }
}

这样可行,但我还想返回组中用户的总数以及属于该组所有权的联系人总数。

伪代码啊!

    /// <summary>
    /// Return summary details about the groups a user belongs to
    /// </summary>
    /// <param name="userId"></param>
    /// <returns></returns>
    public 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
                  {
                      Name = g.Name,
                      CreatedOn = g.CreatedOn,
                      // ### This is the SQL I would write to get the data I want ###
                      MemberCount = ( SELECT COUNT(*) FROM GroupUser AS GU WHERE GU.GroupId = g.GroupId ),
                      ContactCount = ( SELECT COUNT(*) FROM Contact AS C WHERE C.OwnerGroupId = g.GroupId )
                     // ### End of extra code ###
                  }).ToList<Group>();


        return groups;
    }
}

1 个答案:

答案 0 :(得分:4)

我在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;
    }