我正在尝试在linq中实现多级分组
在sql中就是这样的。
Select VarId, variable, UT ,
(case when resultat >= 0 then sum(resultat) else 0) as Positive,
(case when resultat < 0 then sum(resultat) else 0) as Negatif
from
(Select VarId, variable, UT , valeur, sum( resultat ) from IndicRow
group by VarId, variable, UT , valeur)
group by VarId, variable, UT
我已经在linq中创建了但是我找不到按组分组的方法
var Chartline = from IndicRow in Indic_dt.AsEnumerable()
//where SelTradeId.Contains(IndicRow.Field<int>("TradeNum"))
group IndicRow by new
{
GpVariable = IndicRow.Field<string>("Variable"),
GpUT = IndicRow.Field<string>("UT"),
GPVarId = IndicRow.Field<int>("VarId"),
GPValue = IndicRow.Field<double>("Valeur")
} into RdVal
select new
{
VarId = RdVal.Key.GPVarId,
Variable = RdVal.Key.GpVariable,
UT = RdVal.Key.GpUT,
Valeur = RdVal.Key.GPValue,
Resultat = RdVal.Sum(IndicRow => IndicRow.Field<double>("Resultat_EuroNet"))
};
最终目标是获得5列数据表(VarId,变量,UT,正数,负数)
答案 0 :(得分:1)
两级分组的一般模式是
from t in Mains
group t by t.GroupProperty1 into mainGroup
select new { mainGroup.Key,
MainCount = mainGroup.Count(),
subGroups = from mg in mainGroup
group mg by mg.GroupProperty2 into subGroup
select new { subGroup.Key,
SubCount = subGroup.Count()
}
}
因此,主要分组由VarId, variable, UT
和子分组仅由valeur
组成,因为子组仅包含一个主要组的项目。