Quantcast
Viewing latest article 16
Browse Latest Browse All 38

iqueryable with sub query as outer join

I have a sample to look into Async calls and i need to get a count from the sub query. I know how to write this in a TSQL query but i am bit confused with the iqueryable use.

Here is what i currently have. I am getting the users and then get the count inside a loop. How can i do the loop part in the first query?

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();        }    }}

Viewing latest article 16
Browse Latest Browse All 38

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>