New Bee

New Bee

  • NA
  • 52
  • 1.6k

Need help doing a Group count with a join

Jul 3 2019 12:37 PM
 Hi all, I hope someone could help me out with this linq query. 
 I am new to Linq and having a hard time figuring out how to do the 
CountSoldByColor. What I need is to be able to do the count where from the grouped items and alo include a join table Color where the Name of the color is say "blue".
 If you need more info please don't hestiate to ask. Thanks in advance!
 
Cars
----
ID
ModelID
Year
Status
Color
----
ID
CarID
Name
 
  1. from s in   
  2. (  
  3.     from t in Cars  
  4.     select new { t }  
  5. ) group s by new { s.t.ModelID, s.t.Year } into g  
  6.   
  7. select new   
  8. {  
  9.     Model = (from k in Models where k.ID == g.Key.ModelID select k.Name),  
  10.     Year = g.Key.Year,  
  11.     CountSold = g.Count(p => (p.t.Status == "Sold")),   
  12.     // Need to Count the number of Cars that were Sold and is Color blue (lets say for example and where Color is another table)  
  13.     CountSoldByColor =   
  14. }

Answers (3)