c# - LINQ GroupBy 3 properties? -
i've got following class:
public class productinventory : entity { public virtual product product { get; set; } public datetime expirationdate { get; set; } public manager manager { get; set; } }
and class used in viewmodels looks so:
public class productinventorycountmodel { public product product { get; set; } public datetime expirationdate { get; set; } public int count { get; set; } }
i want output of dictionary<manager, list<productinventorycountmodel>
displays products manager , expiration date can print out actual data looking this:
managerbob --producta, expires 8/15/13, count: 10 --producta, expires 10/10/13, count: 40 --productb, expires 6/13/13, count: 30 managertim --producta, expires 10/10/13, count: 5 --productb, expires 5/25/13, count: 10 --productb, expires 6/13/13, count 40
how write query in linq when starting list of productinventory
? tried using multiple .groupby
didn't work.
you stated want dictionary<manager, list<productinventorycountmodel>>
, think you'd have this:
var dictionary = db.productinventory.groupby(x => new { x.manager, x.product, x.expirationdate }) .todictionary(g => g.key.manager, g => g.select(x => new productinventorycountmodel { product = x.key.product, expirationdate = x.key.expirationdate, count = x.count() }).tolist());
Comments
Post a Comment