无法在C#中隐式转换类型&#39; System.Collections.Generic.List <anonymoustype#1>

时间:2015-10-08 21:06:36

标签: c# linq

您好我是编程新手,请您帮我解决这个问题:

我在&#34; To.List()&#34;中收到错误     错误1无法隐式转换类型&#39; System.Collections.Generic.List&#39;到&#39; System.Collections.Generic.List

我的代码是:

List<IncByYrMonthModel> GroupedList = (from p in incidentsViewModel
                                       group p by new 
                                       { 
                                           month = p.DateClosed.Value.Month, 
                                           year = p.DateClosed.Value.Year 
                                       } into d
                                       select new 
                                       {
                                           d.Key.month, 
                                           d.Key.year, 
                                           count = d.Count() 
                                        }).ToList();
return GroupedList;

我的模特是:

public class IncByYrMonthModel
{
    public string Year { get; set; }
    public string Month { get; set; }
    public string Total { get; set; }
}

更新了代码

public List<IncByYrMonthModel> GetYrMonth2(HttpSessionStateBase session, int id, int _StrYear)
{
    List<IncidentsModel> incidentsViewModel = ViewModelService.Fetch<List<IncidentsModel>>(session, id);

    List<IncByYrMonthModel> GroupedList = (from p in incidentsViewModel
                                           group p by new 
                                           { 
                                               month = p.DateClosed.Value.Month, 
                                               year = p.DateClosed.Value.Year 
                                            } into d
                                            select new IncByYrMonthModel 
                                            { 
                                                Month = d.Key.month, 
                                                Year = d.Key.year, 
                                                Total = d.Count() 
                                            });

        return GroupedList;
    }

2 个答案:

答案 0 :(得分:5)

你应该改变这个

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
        int sales = 0, sale = 0; //Identfy Variables

        for (int i = 1; i <= 5; i++) {
                cout << "Enter today's sales for Store " << i << ":" << endl;
                cin >> sale;
                sales += sale;
                sales /= 100; //in a similar manner to +=, /= can be used for division.
        }

        cout << "SALES BAR CHART:" << endl;
        cout << "Each * = $100" << endl;

                for (int x = 1; x <= 5; x++) {
                        cout << "Store " << x << ": ";
                        for (int s = 0; s < sales; s++) cout << "*";
                        cout << endl;
                }

         return 0;
}

到这个

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

第一种情况的问题是你有一个匿名类型的对象序列,你尝试将它们转换为select new IncByYrMonthModel { Month = d.Key.month, Year = d.Key.year, Total = d.Count() } 列表,这是不可能的。

答案 1 :(得分:0)

您基本上是在尝试分配匿名类型

  

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

到你的对象。而不是选择新的{...},只需选择新的IncByYrMonthModel(){...}来填充您的对象。