Group by Inside Group by Using EntityFramwork in Mvc5

Dec 13 2018 4:30 AM
Hi guys I am trying I try to bring the count for each male and female by age As in the table
 
 
 
I was able to calculate all ages but I can not count the ages as boys and girls like this
 
here is my code in controller
  1. public ActionResult AllCuont()  
  2. { var query = (from t in db.Pations  
  3. let range = (  
  4. t.Age>= 1 && t.Age < 5 ? "age from 1 to 4" :  
  5. t.Age >= 5 && t.Age < 15 ? "age from 5 to 14" :  
  6. t.Age >= 15 && t.Age < 25 ? "age from 15 to 24" :  
  7. t.Age >= 25 && t.Age < 45 ? "age from 25 to 44" : ""  
  8. ) group t by range into g  
  9. select new UserRange { AgeRange = g.Key, Count = g.Count() }).ToList();  
  10. query.Add(new UserRange() { AgeRange = "Sum",Count = query.Sum(c => c.Count) });  
  11. ViewBag.UserData = query; return View(db.Pations); }  
and my modal pation like this
  1. namespace app.Models  
  2. {  
  3. public class Pation  
  4. {  
  5. [Key]  
  6. public int Id { getset; }  
  7. public string PationName { getset; }  
  8. public int Age { getset; }  
  9. public Sex Sex { getset; }  
  10. public ApplicationUser User { getset; }  
  11. }  
  12. public enum Sex  
  13. {  
  14. boys,  
  15. girls,  
  16. }  
  17. }  
and this is table for count my value
  1. public class UserRange  
  2. {  
  3. public string AgeRange { getset; }  
  4. public int Count { getset; }  
  5. }  
my question is how Display count for each male and female by age

Answers (2)