LinQ 结果进入列表

我有以下类作为列表;


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>'

正在转换为列表();不起作用。


慕娘9325324
浏览 93回答 3
3回答

Helenr

也使用代替任意值类型:当您实例化list_TA时,它需要DateTime SAMPLE_TIME,字符串WAIT_CLASS,双倍计数作为参数传递。为了解决这个问题,请引入无参数构造函数。new list_TA&nbsp;public class list_TA{&nbsp; &nbsp; public DateTime SAMPLE_TIME { get; set; }&nbsp; &nbsp; public string WAIT_CLASS { get; set; }&nbsp; &nbsp; public double COUNT { get; set; }&nbsp; &nbsp; public list_TA()&nbsp; &nbsp; {&nbsp; &nbsp; }&nbsp; &nbsp; public list_TA(DateTime SAMPLE_TIME, string WAIT_CLASS, double COUNT)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.SAMPLE_TIME = SAMPLE_TIME;&nbsp; &nbsp; &nbsp; &nbsp; this.WAIT_CLASS = WAIT_CLASS;&nbsp; &nbsp; &nbsp; &nbsp; this.COUNT = COUNT;&nbsp; &nbsp; }}

慕仙森

您可以在 LINQ 中选择list_TA,如下所示:var test = listASH&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(g => new&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SAMPLE_TIME = statiClass.By15Seconds(Convert.ToDateTime(g.SAMPLE_TIME)),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WAIT_CLASS = g.WAIT_CLASS,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COUNT = 0,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).GroupBy(x => new { x.SAMPLE_TIME, x.WAIT_CLASS })&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(y => new list_TA&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SAMPLE_TIME = y.Key.SAMPLE_TIME,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WAIT_CLASS = y.Key.WAIT_CLASS,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; COUNT = Math.Round(y.Count() / 15.0, 2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList();

慕斯709654

匿名类不能隐式转换为任何其他类型。您需要使用而不是new list_TAnew将默认构造函数添加到类中,并使用以下代码list_TA&nbsp; .Select(y => new list_TA&nbsp; {&nbsp; &nbsp; &nbsp; SAMPLE_TIME = y.Key.SAMPLE_TIME,&nbsp; &nbsp; &nbsp; WAIT_CLASS = y.Key.WAIT_CLASS,&nbsp; &nbsp; &nbsp; COUNT = Math.Round(y.Count() / 15.0, 2),&nbsp; });或&nbsp; .Select(y => new list_TA (&nbsp; &nbsp; &nbsp; y.Key.SAMPLE_TIME,&nbsp; &nbsp; &nbsp; y.Key.WAIT_CLASS,&nbsp; &nbsp; &nbsp; Math.Round(y.Count() / 15.0, 2)&nbsp; &nbsp;));
打开App,查看更多内容
随时随地看视频慕课网APP