动态/有条件地添加多个LINQ GroupBy

时间:2012-02-06 19:38:24

标签: linq dynamic group-by conditional

我有一个用户界面,提供用户选择

  1. 表类型(在几乎相同列集的两个表之间进行选择)
  2. 操作(总和,平均值,中位数等)
  3. GroupBy选项(一堆复选框)
  4. 我打算创建一个基于用户选择返回结果集的方法,虽然我认为我完成了其他所有事情(除了表选择),但我仍然坚持如何使用GroupBy(或任何其他方式)通过一个接一个的条款来建立小组。

    这是现在唯一让我想回到使用简单的SQL字符串生成来做这件事的事情。

    这是我到目前为止的代码:

    public IQueryable<ACSImpactAnalysisModel> GetDataForReport(TableType table, uint year,     OpType operation, uint groupOptions, uint measures)
        {
            var dataContext = new ACSImpactEntities();
    
            var discharges = from p in dataContext.ACSNationals
                             select new 
                             {
                                 p.YEAR,
                                 p.AGE,
                                 p.FEMALE,
                                 p.RACE,
                                 p.ASOURCE,
                                 p.PAY1,
                                 LOS = p.LOS > 0 ? p.LOS : 0,
                                 CHG = p.TOTCHG > 0 ? p.TOTCHG : 0,
                                 NPR = p.NPR > 0 ? p.NPR : 0,
                                 DIS_STS = p.DISPUB04 > 0 ? p.DISPUB04 : 0
                             };
    
            //if (table == TableType.states)
            //    discharges = from p in dataContext.ACSStates
            //                 select p;
    
    
            List<int> years = new List<int>();
            if ((year & Constants.YEAR2008) == Constants.YEAR2008) years.Add(2008);
            if ((year & Constants.YEAR2009) == Constants.YEAR2009) years.Add(2009);
    
            discharges = discharges.Where(a => years.Any(b => a.YEAR == b));
    
            if ((groupOptions & Constants.SEX) == Constants.SEX)
                discharges = discharges.Where(a => a.FEMALE > 0);
    
            if ((groupOptions & Constants.RACE) == Constants.RACE)
                discharges = discharges.Where(a => a.RACE > 0);
    
            if ((groupOptions & Constants.ER_ADMISSION) == Constants.ER_ADMISSION)
                discharges = discharges.Where(a => a.ASOURCE > 0);
    
            if ((groupOptions & Constants.PAYER_TYPE) == Constants.PAYER_TYPE)
                discharges = discharges.Where(a => a.PAY1 > 0);
    
            var analysis = from a in discharges
                           select new
                           {
                               AGE_GROUP = 
                               (
                                    a.AGE >= 18 && a.AGE <= 44 ? "18-44" :
                                    a.AGE >= 45 && a.AGE <= 54 ? "45-54" :
                                    a.AGE >= 55 && a.AGE <= 64 ? "55-64" : ">= 65"
                               ),
                               SEX = (a.FEMALE == 1 ? "FEMALE" : "MALE"),
                               RACE = 
                               (
                                    a.RACE == 1 ? "WHITE" :
                                    a.RACE == 2 ? "BLACK" :
                                    a.RACE == 3 ? "HISPANIC" :
                                    a.RACE == 4 ? "ASIAN OR PACIFIC ISLANDER" :
                                    a.RACE == 5 ? "NATIVE AMERICAN" :
                                    a.RACE == 6 ? "OTHER" : ""
                               ),
                               ASOURCE = (a.ASOURCE == 1 ? "ER" : "NON ER"),
                               PAY1 =
                               (
                                    a.PAY1 == 1 ? "MEDICARE" :
                                    a.PAY1 == 2 ? "MEDICAID" :
                                    a.PAY1 == 3 ? "PRIVATE INCLUDE HMO" :
                                    a.PAY1 == 4 ? "SELF PAY" :
                                    a.PAY1 == 5 ? "NO CHARGE" :
                                    a.PAY1 == 6 ? "OTHER" : ""
                               ),
                                a.YEAR,
                                a.FEMALE,
                                a.LOS,
                                a.CHG,
                                a.NPR,
                                a.DIS_STS
                           };
    
            var grouped_analysis = analysis.GroupBy(groups => groups.YEAR);
    
            //These lines generate error, but this is something what I want to be able to  do
            if ((groupOptions & Constants.AGE_GROUP) == Constants.AGE_GROUP)
                grouped_analysis = analysis.GroupBy(age_group => age_group.AGE_GROUP);
    
            if ((groupOptions & Constants.ASOURCE) == Constants.ASOURCE)
                grouped_analysis = analysis.GroupBy(asource => asource.ASOURCE);
    
    
            return analysis;
        }
    

1 个答案:

答案 0 :(得分:0)

您可以使用Dynamic Linq,也可能需要Predicate Builder