0

我有下表

Date              GRP                Open

1/1/2011         A                    2
1/1/2011         B                    3
2/2/2011         C                    5

组列是一些名为“组即“A + B= Y 和 C 构成 Z”的一部分

我需要按日期和 GRP 分组的 Open 的总和分别作为列

Date              Y     Z     

 1/1/2011         5     0
 2/2/2011         0     5

以下是我尝试过的代码

List<string> Y = new List<string>() { "A", "B" };
List<string> C = new List<string>() { "Z" };
from item in temp group item by new { RD = item.Field<DateTime>("date"),
Group= 
(Y.Contains(item.Field<string>("GRP")) ? "Y" :
 C.Contains(item.Field<string>"GRP")) ? "Z" :                                                 
                              }
                                             into grptbl
                                             select new
                                             {
                                                 RD = grptbl.Key.RD,
                                               A=grptbl.Where(x=>x.Field<string>("Group")=="Y").Sum(x=>x.Field<Int64>("Open")) 
,
                 Z= grptbl.Where(x=>x.Field<string>("Group")=="Z").Sum     (x=>x.Field<Int64>("Open")) };

看起来不错,但它抱怨“组”列在 grptable 中不存在,在 select 子句中

4

1 回答 1

0

回答了

List<string> Y = new List<string>() { "A", "B" };
List<string> C = new List<string>() { "Z" };
from l in (from item in temp group item by new { 
 RD = item.Field<DateTime>("date"),
 Y=Y.Contains(item.Field<string>("GRP")) ? "Y" :item.Field<Int64>("open") : 0,
 Z=C.Contains(item.Field<string>"GRP")) ? "Z":item.Field<Int64>("open") :0                                                 
                          }
                                         into grptbl
                                         select new
                                         {
                                             RD = grptbl.Key.RD,
                                           Y=grptbl.Key.Y,
                                           Z=grptbl.Key.Z
                                          } )
    group l by new {l.RD} into g
    {
     RD=g.Key.RD,
     Y=g.Sum(x=>x.Y),
     Z=g.Sum(x=>x.Z)};
于 2013-02-19T15:35:20.883 回答