亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

LinQ 結果進入列表

LinQ 結果進入列表

C#
慕娘9325324 2022-08-20 17:24:25
我有以下類作為列表;class list_TA{    public DateTime SAMPLE_TIME { get; set; }    public string WAIT_CLASS { get; set; }    public double COUNT { get; set; }    public list_TA(DateTime SAMPLE_TIME, string WAIT_CLASS,double COUNT)    {        this.SAMPLE_TIME = SAMPLE_TIME;        this.WAIT_CLASS = WAIT_CLASS;        this.COUNT = COUNT;    }}//SECOND PART                 var test = listASH                          .Select(g => new                          {                              SAMPLE_TIME = statiClass.By15Seconds(Convert.ToDateTime(g.SAMPLE_TIME)),                              WAIT_CLASS = g.WAIT_CLASS,                              COUNT = 0,                          }).GroupBy(x => new { x.SAMPLE_TIME, x.WAIT_CLASS })                          .Select(y => new                          {                              SAMPLE_TIME = y.Key.SAMPLE_TIME,                              WAIT_CLASS = y.Key.WAIT_CLASS,                              COUNT = Math.Round(y.Count() / 15.0, 2),                          });我想要的是將linq結果加載到list_TA中。但是下面的代碼不起作用,它給出了以下錯誤; List<list_TA> lst = (List<list_TA>)test.ToList(); 錯誤;Cannot convert type 'System.Collections.Generic.List<<anonymous type: System.DateTime SAMPLE_TIME, string WAIT_CLASS, double COUNT>>' to 'System.Collections.Generic.List<vodaMon.list_TA>'正在轉換為列表();不起作用。
查看完整描述

3 回答

?
Helenr

TA貢獻1780條經驗 獲得超4個贊

也使用代替任意值類型:當您實例化list_TA時,它需要DateTime SAMPLE_TIME,字符串WAIT_CLASS,雙倍計數作為參數傳遞。為了解決這個問題,請引入無參數構造函數。new list_TA


 public class list_TA

{

    public DateTime SAMPLE_TIME { get; set; }

    public string WAIT_CLASS { get; set; }

    public double COUNT { get; set; }


    public list_TA()

    {


    }


    public list_TA(DateTime SAMPLE_TIME, string WAIT_CLASS, double COUNT) 

    {

        this.SAMPLE_TIME = SAMPLE_TIME;

        this.WAIT_CLASS = WAIT_CLASS;

        this.COUNT = COUNT;

    }

}


查看完整回答
反對 回復 2022-08-20
?
慕仙森

TA貢獻1827條經驗 獲得超8個贊

您可以在 LINQ 中選擇list_TA,如下所示:


var test = listASH

                          .Select(g => new

                          {

                              SAMPLE_TIME = statiClass.By15Seconds(Convert.ToDateTime(g.SAMPLE_TIME)),

                              WAIT_CLASS = g.WAIT_CLASS,

                              COUNT = 0,

                          }).GroupBy(x => new { x.SAMPLE_TIME, x.WAIT_CLASS })

                          .Select(y => new list_TA

                          {

                              SAMPLE_TIME = y.Key.SAMPLE_TIME,

                              WAIT_CLASS = y.Key.WAIT_CLASS,

                              COUNT = Math.Round(y.Count() / 15.0, 2),

                          }).ToList();


查看完整回答
反對 回復 2022-08-20
?
慕斯709654

TA貢獻1840條經驗 獲得超5個贊

匿名類不能隱式轉換為任何其他類型。


您需要使用而不是new list_TAnew


將默認構造函數添加到類中,并使用以下代碼list_TA


  .Select(y => new list_TA

  {

      SAMPLE_TIME = y.Key.SAMPLE_TIME,

      WAIT_CLASS = y.Key.WAIT_CLASS,

      COUNT = Math.Round(y.Count() / 15.0, 2),

  });


  .Select(y => new list_TA (

      y.Key.SAMPLE_TIME,

      y.Key.WAIT_CLASS,

      Math.Round(y.Count() / 15.0, 2)

   ));


查看完整回答
反對 回復 2022-08-20
  • 3 回答
  • 0 關注
  • 118 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號