无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Collections.Generic.List'。存在显式转换(您是否错过了演员?) [英] Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

查看:600
本文介绍了无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Collections.Generic.List'。存在显式转换(您是否错过了演员?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程声明:

I have the following class declaration:

<blockquote class="FQ"><div class="FQA">Quote:</div> public class EventGroup
    {
        private readonly Guid id;
        private readonly string name;
        private readonly string description;
        private readonly Guid schoolId;     
       
        public EventGroup(Guid id, string name, string description, Guid schoolID) 
        {
            this.id = id;          
            this.schoolId = schoolID;
            this.name = name;
            this.description = description;
        }      
       public Guid SchoolID
        {
            get { return schoolId; }
        }
        public string Description
        {
            get { return description; }
        }
        public string Name
        {
            get { return name; }
        }
        public Guid ID
        {
            get { return id; }
        }       
       
    }





当我声明以下工作时



When I declare the following its working

<blockquote class="FQ"><div class="FQA">Quote:</div>

List<EventGroup> lstGroup = new List<EventGroup>();
var eventt_grp= lstGroup.Select(r => new {r.ID,r.Name});





但如果不能正常工作我喜欢这样并得到错误:



But its not working if I do like this and getting the error:

Quote:

无法隐式转换类型'System.Collections.Generic.IEnumerable< anonymoustype#1>'到'System.Collections.Generic.List< tools.businessobjects.eventgroup>'。存在一个

显式转换(您是否错过了演员表?)

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<anonymoustype#1>' to 'System.Collections.Generic.List<tools.businessobjects.eventgroup>'. An
explicit conversion exists (are you missing a cast?)







<blockquote class="FQ"><div class="FQA">Quote:</div>

List<EventGroup> eventt_grp1 = lstGroup.Select(r => new {r.ID, r.Name });

推荐答案

使用var并且根本不创建列表。您需要在Select的末尾调用ToList以获取列表,而不是IEnumerable。



List< eventgroup> eventt_grp1 = lstGroup.Select(r => new {r.ID,r.Name})。ToList();



如果你不想使用var,您需要定义一个具有ID和Name属性的类。您需要使用var,因为您正在使用匿名类。
Use var and don't create a list at all. You need to call ToList at the end of your Select in order to get back a list, instead of an IEnumerable.

List<eventgroup> eventt_grp1 = lstGroup.Select(r => new {r.ID, r.Name }).ToList();

If you don't want to use var, you need to define a class that has ID and Name properties. You need to use var because you're using an anonymous class.


这是因为您从<返回匿名类型code>选择并尝试将其存储在列表< eventgroup> 中。预测总是创建匿名类型。



此外,如果您只需要 ID 和<$ c $结果中的c> name 为什么要将它们存储在 List< eventgroup> 列表中。使用 var 应该没问题。如果你必须在列表< eventgroup> 中得到结果,那么就会出现问题,你可能想重新考虑逻辑/设计。



如果你不想使用var那么你将不得不利用这样的事实,即返回的任何内容都是可枚举的。所以试试这个,这应该有效。

This is because you are returning an anonymous type from your Select and you are trying to store it in the List<eventgroup>. The projections always create anonymous types.

Also, if you only need to have ID and name in the results why are you storing them in a list of List<eventgroup>. using var should be fine. If you must have the result in the List<eventgroup> then there is something wrong and you might want to reconsider the logic/design.

If you don't want to use var then you will have to utilize the fact that utlimately whatever is being returned is enumerable. so try this and this should work.
IEnumerable eventt_grp = lstGroup.Select(r => new {r.ID, r.Name });


这篇关于无法将类型'System.Collections.Generic.IEnumerable'隐式转换为'System.Collections.Generic.List'。存在显式转换(您是否错过了演员?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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