Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple join with groupby in linq

Tags:

c#

linq

i have 5 table from which i want to get some information using linq.i am using following query for reading data from data .

var query = (from GRD in _tblStudents.GetQueryable() 
             join apt in _tblApplicants.GetQueryable() 
                  on GRD.ApplicantID equals apt.ApplicantID
             join cls in _tblClasses.GetQueryable() 
                  on GRD.CityID equals cls.ClassID
             join prg in _tblPrograms.GetQueryable() 
                  on cls.ProgramID equals prg.ProgramID
             join city in _tblCities.GetQueryable() 
                  on GRD.CityID equals city.CityID
             where GRD.AcademicYearID == yearId && cls.ProgramID == programId
             group apt by new{apt.Gender} into grouped
             select new CityWiseStudentModel
             {
                  CityName=city.CityName, 
                  //'city' does not exist in the current context
                  Gender = grouped.Count(),
                  programName=prg.Program, 
                 //'prg' does not exist in the current context
              }   

           );

How i can get City name from city table and program name from prg table

like image 726
Muhammad Nasir Avatar asked Jul 14 '15 06:07

Muhammad Nasir


Video Answer


1 Answers

group <--> by <--> into <--> is changed your scope to IGrouping<a,b>

My opinion is not only apt.Gender is your key but city.CityName and prg.Program

try this (or some similar):

group apt by new{apt.Gender, city, prg} into grouped
select new CityWiseStudentModel
{
     CityName = grouped.Key.city.CityName, 
     Gender = grouped.Count(), //rename GenderCount
     programName = grouped.Key.prg.Program,
     // Gender = grouped.Key.Gender,
} 
like image 193
sac1 Avatar answered Sep 29 '22 06:09

sac1