2012-02-06 64 views
1

我有提供 動態/有條件地將多個LINQ的GroupBy

  • 操作(總和(幾乎相同的柱組的兩個表之間的選擇)的

    1. 表類型用戶選擇的用戶接口,平均值,中位數等)
    2. 的GroupBy選項(一堆複選框)

    我打算創建retur的方法ns結果集基於用戶選擇,雖然我認爲我通過了其他一切(除了表選擇),但我堅持如何使用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; 
        } 
    
  • +0

    這是EF還是Linq2Sql? – 2012-02-06 21:32:48

    +0

    即時通訊使用LINQ to Entities – 2012-02-06 22:25:46

    回答

    0

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

    +0

    關於動態Linq,它不會打敗編寫類型安全查詢的整個目的嗎?至於Predicate Builder,我無法理解如何將多個條件分組添加到結果集中。它似乎更適合建設動態的地方/過濾條款 – 2012-02-06 23:38:03

    +0

    是的,確實如此。但是你可以用單元測試來處理這個問題。 – 2012-02-06 23:39:23

    +0

    在處理這種類型的查詢時,很好的瞭解Predicate Builder(是的,適用於Where) – 2012-02-06 23:42:21