group table by foreign key table in mvc5 using linq

Jan 23 2019 11:02 AM

hi How are you guys? I need some help Let me explain to you in the first. I have a table with a group of patients per month that means it's a month data compilation process I want to collect patient data per month according to type of disease i mean a total number of patients per month, depending on the type of disease I tried to collect the data by month but I can't collect the data by type of disease here I need your help

my pation table like this
  1. {  
  2.     public class Pation  
  3.     {  
  4.         [Key]  
  5.         public int Id { getset; }  
  6.         public string PationName { getset; }  
  7.         public int Age { getset; }  
  8.         public Sex Sex { getset; }  
  9.         public DateTime DateWared { getset; } = DateTime.UtcNow.AddHours(3);  
  10.         public int Type_diseaseId { getset; }  
  11.         [ForeignKey("Type_diseaseId")]  
  12.         public virtual Type_disease Type_diseases { getset; }  
  13.         public ApplicationUser User { getset; }  
  14.     }  
  15.     public enum Sex  
  16.     {  
  17.         boys,  
  18.         girls,  
  19.     }  
  20. }  
type of disease model which is the foreign key of pation which is i want to group by it's like this
  1. {  
  2.     public class Type_disease  
  3.     {  
  4.         [Key]  
  5.         public int Id { getset; }  
  6.         public string Namedisease { getset; }  
  7.         public virtual ICollection  Pations {  getset; }  
  8.     
  9.     }  
  10. }  

i grouped the pation by age an sum values in the model i called

UserRing and it's like this

  1. {  
  2.     public class UserRange  
  3.     {  
  4.         public string AgeRange { getset; }  
  5.         public int Count { getset; }  
  6.         public string Gender { getinternal set; }  
  7.         public string grilsTotal { getinternal set; }  
  8.         public string boysTotal { getinternal set; }  
  9.         public string Type_d { getinternal set; }  
  10.     }  
  11.        
  12. }  
and i add actionResult named ShowPation to group by the result of pation like this
  1. public ActionResult ShowPation()  
  2.      {  
  3.          var query1 = from t in db.Pations()  
  4.                       let Agerange =  
  5.                        (  
  6.                          t.Age >= (0) && t.Age < (1) ? "Under year" :  
  7.                          t.Age >= (1) && t.Age < (5) ? "1 _ 4" :  
  8.                          t.Age >= (5) && t.Age < (15) ? "5 _ 14" :  
  9.                          t.Age >= (15) && t.Age < (25) ? "15 _ 24" :  
  10.                          t.Age >= (25) && t.Age < (45) ? "24 _ 44" :  
  11.                          "over 45"  
  12.                        )  
  13.                       let Sex = (t.Sex == 0 ? "boys" : "girls")  
  14.                       group new { t.Age, t.Sex, Agerange } by new { t.DateWared.Year, t.DateWared.Month, Agerange, Sex } into g  
  15.                       select g;  
  16.     
  17.                   var query2 = from g in query1  
  18.                       select new { mycount = g.Count(), g.Key.Year, g.Key.Month, g.Key.Agerange, g.Key.Sex };  
  19.     
  20.                  var query3 = from i in query2  
  21.                       group i by new { i.Year, i.Month} into g  
  22.                       select g;  
  23.     
  24.          Dictionary<string, List> urn = new Dictionary<string, List>();  
  25.          foreach (var item in query3)  
  26.          {  
  27.              foreach (var item1 in item)  
  28.              {  
  29.                  if (!urn.ContainsKey(item.Key.Month + "/" + item.Key.Year))  
  30.                  {  
  31.                      urn[item.Key.Month + "/" + item.Key.Year] = new List();  
  32.                  }  
  33.                  urn[item.Key.Month + "/" + item.Key.Year].Add(new UserRange { Count = item1.mycount, AgeRange = item1.Agerange, Gender = item1.Sex });  
  34.     
  35.              }  
  36.              urn[item.Key.Month + "/" + item.Key.Year] = urn[item.Key.Month + "/" + item.Key.Year].OrderByDescending(i => i.AgeRange).ToList();//sort the data according to Agerange  
  37.          }  
  38.          return View(urn);  
  39.      }  
with view like this
  1. @model  Dictionary<string, List>  
  2.          "1"  class="table table-responsive table-bordered table-hover table-striped " style="height: 145px;text-align: center; border: solid 1px;border-radius: 5px;direction: rtl;">  
  3.             @foreach (string key in Model.Keys)  
  4.             {  
  5.                
  6.                  "17"
    @key
      
  7.                
  8.                                    
  9.                  
  10.                    @foreach (The_Hospital_Project.Models.UserRange item1 in Model[key])  
  11.                    {  
  12.                         "height: 57px;" >@item1.AgeRange  
  13.                    }  
  14.                  
  15.                               
  16.                    
  17.                    @foreach (The_Hospital_Project.Models.UserRange item1 in Model[key])  
  18.                    {  
  19.                        @item1.Gender  
  20.                    }  
  21.                  
  22.               
  23.               
  24.                 @foreach (The_Hospital_Project.Models.UserRange item1 in Model[key])  
  25.                 {  
  26.                     @item1.Count  
  27.                 }  
  28.               
  29.    
  30.             }  
  31.           

The table simply collects all the records every month.
What I want is to sort the records by type of disease
each month , for So far, The code I have works well and collects records every month but I can not sort by type of diseases within each month that's all  .

What I want is to collect data by disease within each month depending on the type of disease model which is the foreign key of pation to be the result like in each month this Painted image I hope explain what I want It's really what I want
 

Answers (2)