简体   繁体   中英

Entity framework GroupBy conditionally at C#

I want to write entity framework query. But my groupping is not static, it is dynamically by conditions. My query like below. But, I couldn't give any default value to grouppedData . Because it's changing for every condition. How can I achieve this?

var grouppedData= ??????;

if (isCountrySelected && !isStateSelected && !isYearSelected)
{
   grouppedData = context.Students.GroupBy(p => new { p.country });
}
else if (!isCountrySelected && isStateSelected && !isYearSelected)
{
   grouppedData = context.Students.GroupBy(p => new { p.state });
}
else if (!isCountrySelected && !isStateSelected && isYearSelected)
{
   grouppedData = context.Students.GroupBy(p => new { p.year });
}
else if (isCountrySelected && isStateSelected && !isYearSelected)
{
   grouppedData = context.Students.GroupBy(p => new { p.country,p.state });
}
else if (!isCountrySelected && isStateSelected && isYearSelected)
{
   grouppedData = context.Students.GroupBy(p => new { p.state, p.year });
}
else if (isCountrySelected && !isStateSelected && isYearSelected)
{
   grouppedData = context.Students.GroupBy(p => new { p.country, p.year });
}
else if (isCountrySelected && isStateSelected && isYearSelected)
{
   grouppedData = context.Students.GroupBy(p => new { p.country, p.state, p.year });
}


var result= grouppedData.Select(p => new
   {
       PrimaryStudentCount = p.Sum(k => k.PrimaryStudent),
       SecondaryStudentCount = p.Sum(k => k.SecondaryStudent),
       UniversityStudentCount = p.Sum(k => k.UniversityStudent),
       MaleStudentCount = p.Sum(k => k.MaleStudent),
       FemaleStudentCount = p.Sum(k => k.FemaleStudent),
       .
       .
       .
    }).ToList();

Try this

IQueryable<IGrouping<object, Student>> groupedData= null;

Your conditions here ...

var result = GetGroupDataResult(groupedData);

Now call the function...

private object GetGroupDataResult(IQueryable<IGrouping<object, Student>> groupedData)
        {
          var result = groupBy.Select(p => new
            {
               PrimaryStudentCount = p.Sum(k => k.PrimaryStudent),
               SecondaryStudentCount = p.Sum(k => k.SecondaryStudent),
               UniversityStudentCount = p.Sum(k => k.UniversityStudent),
               MaleStudentCount = p.Sum(k => k.MaleStudent),
               FemaleStudentCount = p.Sum(k => k.FemaleStudent),
               .
               .
               .
            }).ToList();

           return result;
         }

Try this, use a string key for grouping

var grouppedData=from i in context.Students
                 let mykey = (condition1 ? string.Format(...) :
                              condition2 ? string.Format(...) :
                              condition3 ? string.Format(...) : ...)
                 group by mykey int g
                 select new
                 {
                   PrimaryStudentCount = g.Sum(k => k.PrimaryStudent),
                   SecondaryStudentCount = g.Sum(k => k.SecondaryStudent)
                 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM