group table by foreign key table in mvc5 using linq

141 views Asked by At

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

{
    public class Pation
    {
        [Key]
        public int Id { get; set; }
        public string PationName { get; set; }
        public int Age { get; set; }
        public Sex Sex { get; set; }
        public DateTime DateWared { get; set; } = DateTime.UtcNow.AddHours(3);
        public int Type_diseaseId { get; set; }
        [ForeignKey("Type_diseaseId")]
        public virtual Type_disease Type_diseases { get; set; }
        public ApplicationUser User { get; set; }
    }
    public enum Sex
    {
        boys,
        girls,
    }
}

type of disease model which is the foreign key of pation which is i want to group by it's like this

{
    public class Type_disease
    {
        [Key]
        public int Id { get; set; }
        public string Namedisease { get; set; }
        public virtual ICollection<Pation> Pations { get; set; }

    }
}

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

UserRing and it's like this

{
    public class UserRange
    {
        public string AgeRange { get; set; }
        public int Count { get; set; }
        public string Gender { get; internal set; }
        public string grilsTotal { get; internal set; }
        public string boysTotal { get; internal set; }
        public string Type_d { get; internal set; }
    }

}

and i add actionResult named ShowPation to group by the result of pation like this

public ActionResult ShowPation()
     {
         var query1 = from t in db.Pations()
                      let Agerange =
                       (
                         t.Age >= (0) && t.Age < (1) ? "Under year" :
                         t.Age >= (1) && t.Age < (5) ? "1 _ 4" :
                         t.Age >= (5) && t.Age < (15) ? "5 _ 14" :
                         t.Age >= (15) && t.Age < (25) ? "15 _ 24" :
                         t.Age >= (25) && t.Age < (45) ? "24 _ 44" :
                         "over 45"
                       )
                      let Sex = (t.Sex == 0 ? "boys" : "girls")
                      group new { t.Age, t.Sex, Agerange } by new { t.DateWared.Year, t.DateWared.Month, Agerange, Sex } into g
                      select g;

                  var query2 = from g in query1
                      select new { mycount = g.Count(), g.Key.Year, g.Key.Month, g.Key.Agerange, g.Key.Sex };

                 var query3 = from i in query2
                      group i by new { i.Year, i.Month} into g
                      select g;

         Dictionary<string, List<UserRange>> urn = new Dictionary<string, List<UserRange>>();
         foreach (var item in query3)
         {
             foreach (var item1 in item)
             {
                 if (!urn.ContainsKey(item.Key.Month + "/" + item.Key.Year))
                 {
                     urn[item.Key.Month + "/" + item.Key.Year] = new List<UserRange>();
                 }
                 urn[item.Key.Month + "/" + item.Key.Year].Add(new UserRange { Count = item1.mycount, AgeRange = item1.Agerange, Gender = item1.Sex });

             }
             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
         }
         return View(urn);
     }

with view like this

@model  Dictionary<string, List<The_Hospital_Project.Models.UserRange>>
        <table border="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;">
            @foreach (string key in Model.Keys)
            {
             <tr>
                <td colspan="17"> <center>@key</center></td>
             </tr>
               <tr>                  

                   @foreach (The_Hospital_Project.Models.UserRange item1 in Model[key])
                   {
                       <td style="height: 57px;">@item1.AgeRange</td>
                   }
               </tr>
               <tr>             

                   @foreach (The_Hospital_Project.Models.UserRange item1 in Model[key])
                   {
                       <td>@item1.Gender</td>
                   }
               </tr>
            <tr>

                @foreach (The_Hospital_Project.Models.UserRange item1 in Model[key])
                {
                    <td>@item1.Count</td>
                }
            </tr>

            }
        </table>

The table simply collects all the records every month. What I want is to sort the records by type of disease each month , So far, the code works well to collect data by month

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 image in each month here

how can i do this guys i tried so hard but i can't do this please guys help me

1

There are 1 answers

7
markorial On

What i would do is load all and do the logic first like follows. Group by disease first then I would separate by month and finally group and sum by age, gender or whatever you need. And after i would get the result i wand then refactor it further so it gets data directly from the database with a linq query :) So i would start with something like this

var diseaseGroupByMonth = Pation.GroupBy(x => x.DateWarred.Month);
//might need to set year as well here if you have data for multiple years
var groupDiseaseByMonth = diseaseGroup.GroupBy(x => x.Type_diseaseID)
var group...

Now this might be a bit tedious to do but i find it easier to first get data i want then try and do it better.

edit:

public DiseaseMonthRange ()
{
   public DateTime Month (get; set;)
   public List<DiseaseGroup> Group (get; set;)
}

public DiseaseGroup ()
{
  public int DiseaseId (get; set;)
  public List<UserRange> (get; set;)
}

Then fill those models from groupings and show that data.

Hope this helps