无法隐式转换类型'System.Collections.Generic.List< AnonymousType#1>在C#中 [英] Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1> in C#

查看:548
本文介绍了无法隐式转换类型'System.Collections.Generic.List< AnonymousType#1>在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手,请您帮我一个忙吧

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

我在"To.List()"中遇到错误 错误1无法将类型'System.Collections.Generic.List'隐式转换为'System.Collections.Generic.List

我的代码是:

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

My Code is:

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;
    }

推荐答案

您应该更改此

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() }

第一种情况下的问题是,您有一个匿名类型的对象序列,您尝试将其转换为IncByYrMonthModel列表,这是不可能的.

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.

这篇关于无法隐式转换类型'System.Collections.Generic.List&lt; AnonymousType#1&gt;在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆