简体   繁体   English

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

[英]Linq-to-SQL: How to perform a count on a sub-select

I'm still trying to get my head round how to use LINQ-to-SQL correctly, rather than just writing my own sprocs. 我仍在设法弄清楚如何正确使用LINQ-to-SQL,而不仅仅是编写自己的存储过程。

In the code belong a userId is passed into the method, then LINQ uses this to get all rows from the GroupTable tables matching the userId . 在代码中,将一个userId传递到该方法中,然后LINQ使用它从与该userId匹配的GroupTable表中获取所有行。 The primary key of the GroupUser table is GroupUserId , which is a foreign key in the Group table. GroupUser表的主键是GroupUserId ,它是Group表中的外键。

    /// <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;
    }
}

This works fine, but I'd also like to return the total number of Users who are in a group and also the total number of Contacts that fall under ownership of the group. 这可以正常工作,但是我还想返回组中用户的总数以及该组所有权下的联系人总数。

Pseudo code ahoy! 伪代码嗨!

    /// <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;
    }
}

It seems the LINQ version of what I wrote in SQL there does the trick, I didn't think it would work! 我在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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM