可查询子查询作为外连接

iqueryable with sub query as outer join

我有一个示例来查看异步调用,我需要从子查询中获取计数。我知道如何在 TSQL 查询中编写它,但我对可查询的使用有点困惑。

这是我目前拥有的。我正在获取用户,然后在循环中获取计数。我如何在第一个查询中执行循环部分?

public static async Task GetUsers(this List<UserViewModel> users)
{
    var db = ApplicationDbContext.Create();

    users.AddRange(await (from u in db.Users 
                          select new UserViewModel
                          {
                              Id = u.Id,
                              Email = u.Email,
                              FirstName = u.FirstName, 
                              LastName = u.LastName
                          }).OrderBy(o => o.Email).ToListAsync());

    if (users.Any())
    {
        foreach(var user in users)
        {
            user.SubscriptionsCount = await (from us in db.UserSubscriptions
                                             join s in db.Subscriptions on us.SubscriptionId equals s.Id
                                             where us.UserId.Equals(user.Id)
                                             select us).CountAsync();
        }
    }
}

可以通过以下两种方式之一进行处理。我为我的样本选择了#2。

1 : 带子查询

var singleQuery = from u in db.Users 
                                  join sub in (from us in db.UserSubscriptions  
                                               join s in db.Subscriptions on us.SubscriptionId equals s.Id 
                                               group us by us.UserId into countGroup
                                               select new { Count = countGroup.Count(), UserId = countGroup.Key }) 
                                    on u.Id equals sub.UserId into sub1 
                                  from subR in sub1.DefaultIfEmpty() 
                                  select new UserViewModel
                                  {
                                      Id = u.Id,
                                      Email = u.Email,
                                      FirstName = u.FirstName,
                                      LastName = u.LastName, 
                                      SubscriptionsCount = subR.Count == null ? 0 : subR.Count
                                  };

var siteUsersSub = await (query).OrderBy(o => o.Email).ToListAsync();

2:由子查询组成

var subQuery = from us in db.UserSubscriptions
                                   join s in db.Subscriptions on us.SubscriptionId equals s.Id
                                   group us by us.UserId into countGroup
                                   select new { Count = countGroup.Count(), UserId = countGroup.Key };

var query = from u in db.Users
                                join sq in subQuery on u.Id equals sq.UserId into sq1 
                                from sqR in sq1.DefaultIfEmpty()
                                select new UserViewModel()
                                {
                                    Id = u.Id,
                                    Email = u.Email,
                                    FirstName = u.FirstName,
                                    LastName = u.LastName, 
                                    SubscriptionsCount = sqR.Count == null ? 0 : sqR.Count
                                };

var siteUsers = await(query).OrderBy(o => o.Email).ToListAsync();