c# - LINQ Left Outer join with group by count through extension methods -
i have 2 tables. categories
, ads
.
every category
can have many ads in ads
table.
i want categories details including how many ads have each.
following linq expression fetches categories have ads may bacause firstordefault()
i know how can achieve result given condition plus categories have location id (let say: lid) of "7".
following in expression
var x1 = context.categories .groupjoin( context.ads, cat => cat.id, ad => ad.catid, (cat, ad) => new { cats = cat, ads = ad }) .selectmany( => a.ads.defaultifempty(), (a, y) => new { catss = a.cats, adss = y }) .groupby(w => w.adss,ww=>new { cat=ww.catss,count=ww.catss.ads.count()}) .where(s=>s.firstordefault().cat.lid==7);
you try following:
var result = context.categories .where(category => category.lid == 7) .groupjoin( context.ads , category => category.id , ad => ad.catid , (c,a) => new { category = category, numberofads = a.defaultifempty().count(x => x!=null) });
Comments
Post a Comment