Quantcast
Channel: User learning... - Stack Overflow
Viewing all articles
Browse latest Browse all 38

Answer by learning... for iqueryable with sub query as outer join

$
0
0

Could be handled in one of the following two ways. I have picked #2 for my sample.

1 : with sub query

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: Composing from sub queries

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

Viewing all articles
Browse latest Browse all 38

Trending Articles



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