可能重复:
linq case statement计算领域
请考虑这样的情景:
我有这样的功能:
private void MyFunction(byte Period)
{
...
我在这样的数据库中有一个表格:
... F008 F009_1 F009_2 F009_3 F009_4 F009_5 F009_6 F009_7 F009_8 ...
-------------------------------------------------------------------------------------------------
在函数体中我想做一些计算。
var MyCalculation = from r in ent.MyTable
group r by new { r.F000 } into grp
select new
{
F008 = grp.Sum(o => o.F008)
F009 = ?????
问题是在F009
情况下,我应该在LINQ投影做到这一点:
switch(Period)
{
case 1:
F009 = (Sum of F009_1) + (Sum of F009_2);
break;
case 2:
F009 = (Sum of F009_3) + (Sum of F009_4);
break;
case 3:
F009 = (Sum of F009_5) + (Sum of F009_6);
break;
case 4:
F009 = (Sum of F009_7) + (Sum of F009_8);
break;
}
我怎么能在LINQ投影用这个switch case
?
谢谢