c# - How to count more than one distinct columns in linq to sql? -
i struggling linq, hence lame question still .. have table following 4 columns
pid sku colorid sizeid 1 'a65102' 38 2 2 'a65102' 38 4 3 'a65102' 38 18
i have how many different color , size option available this.
in sql, use by:
select count(distinct [sizeid]) ,count(distinct [colors]) [item_master] sku = 'a65102'
but in linq, can values
var dd = (from im in db.item_masters im.sku.contains("a65102") select new {im.sizeid,im.colors });
but how can count of distinct values both in single query?
you use groupby
:
var distinctcounts = db.item_masters.groupby(im => im.sku) .where(grp => grp.key == "a65102") .select(grp => new { cntsizes = grp.select(im => im.sizeid).distinct().count(), cntcolors = grp.select(im => im.colorid).distinct().count() });
Comments
Post a Comment