Cannot implicitly convert type 'System.Collections.Generic.List in C#

前端 未结 2 889
孤城傲影
孤城傲影 2021-01-22 01:48

Hi I\'m new to programming, could you please help me with this one:

I\'am getting an error in \"To.List()\" Error 1 Cannot implicitly convert type \'System.Co

相关标签:
2条回答
  • 2021-01-22 02:36

    You are basically trying to assign an anonymous type

    new { d.Key.month, d.Key.year, count = d.Count() }

    to your object. Instead of select new { ... }, just select new IncByYrMonthModel() { ... } to populate your object.

    0 讨论(0)
  • 2021-01-22 02:46

    You should just change this

    select new { d.Key.month, d.Key.year, count = d.Count() }
    

    to this

    select new IncByYrMonthModel { Month = d.Key.month, Year = d.Key.year, Total = d.Count() }
    

    The problem in the first case is that you have a sequence of object of an anonymous type, which you try to convert them to a list of IncByYrMonthModel, which is impossible.

    0 讨论(0)
提交回复
热议问题