2017-06-21 66 views
2

我想在多列上使用組,但是,無法理解我該如何選擇SAMPLE_TIME,即使我寫曼紐爾日期來取樣時間,我得到以下錯誤;Linq上的多列組

System.InvalidCastException: 'System.ValueTuple`2[System.String,System.String]' 'System.String' 

目前我使用的是如下所示;

var dt = (from dr1 in dtTopAll.AsEnumerable() 
          where dr1.Field<DateTime>("SAMPLE_TIME") <= maxDate 
          group dr1 by ("WAIT_CLASS", "SAMPLE_TIME") into g 
          select new 
          { 
           SAMPLE_TIME = // How can i select sample_time, 
           WAIT_CLASS = g.Key, 
           WAITS = g.Sum(z => z.Field<double>("WAITS")/100) 
          }).ToDataTable(); 

回答

2

你有沒有嘗試過這樣的:

var dt = (from dr1 in dtTopAll.AsEnumerable() 
      where dr1.Field<DateTime>("SAMPLE_TIME") <= maxDate 
      group dr1 by new 
         { 
          WAIT_CLASS = dr1.Field<string>("WAIT_CLASS"), 
          SAMPLE_TIME = dr1.Field<DateTime>("SAMPLE_TIME") 
         } into g 
      select new 
        { 
         SAMPLE_TIME = g.Key.SAMPLE_TIME, 
         WAIT_CLASS = g.Key.WAIT_CLASS, 
         WAITS = g.Sum(z => z.Field<double>("WAITS")/100) 
        }).ToDataTable(); 
+0

在組內,它會拋出一個錯誤,如'CS0746 C#無效的匿名類型成員聲明符。匿名類型成員必須用成員分配,簡單名稱或成員訪問來聲明。' –

+0

@doctorcesar:對不起,我已更新帖子,請看一下 –

+0

@doctorcesar:爲什麼不接受?如有需要,請隨時要求澄清 –