- 表類型用戶選擇的用戶接口,平均值,中位數等)
- 的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;
}
這是EF還是Linq2Sql? – 2012-02-06 21:32:48
即時通訊使用LINQ to Entities – 2012-02-06 22:25:46