不能含蓄转化类型System.Collections.Generic.IEnumerable< B>到System.Collections.Generic.List< B> [英] Cannot implicity convert type System.Collections.Generic.IEnumerable<B> to System.Collections.Generic.List<B>

查看:282
本文介绍了不能含蓄转化类型System.Collections.Generic.IEnumerable< B>到System.Collections.Generic.List< B>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过下面的代码,我得到这个错误并且需要怎样让方法Load符列表与LT帮助; B>



无法含蓄型转换为System.Collections.Generic.IEnumerable System.Collections.Generic.List

 大众A级
{
公开名单< b>负载(集合科尔)
{
名单< B>名单=从科尔点¯x选择新的B {为prop1 = x.title,Prop2 = x.dept};
返回列表;
}
}

公共类B
{
公共字符串为prop1 {获取;集;}
公共字符串Prop2 {获取;集;}
}


解决方案

您查询将返回的IEnumerable ,而你的方法必须返回一个列表< b> 结果
你可以转换您查询通过了ToList()扩展方法列表的结果。

 大众A级
{
公开名单< b>负载(集合科尔)
{
名单< B>名单=(从X在科尔选择新的B {为prop1 = x.title,Prop2 = x.dept})了ToList()。
返回列表;
}
}



类型列表中应自动被推断编译器。如果不是,你就需要调用&了ToList LT; B>()


With the code below i get this error and need help with how to let method Load return List<B>

Cannot implicity convert type System.Collections.Generic.IEnumerable to System.Collections.Generic.List

public class A
    {
      public List<B> Load(Collection coll)
      {
        List<B> list = from x in coll select new B {Prop1 = x.title, Prop2 = x.dept};
        return list;
      }
    }

public class B
{
  public string Prop1 {get;set;}
  public string Prop2 {get;set;} 
}

解决方案

Your query returns an IEnumerable, while your method must return a List<B>.
You can convert the result of your query to a list via the ToList() extension method.

public class A
{
   public List<B> Load(Collection coll)
   {
       List<B> list = (from x in coll select new B {Prop1 = x.title, Prop2 = x.dept}).ToList();
       return list;
   }
}

The type of the list should be inferred automatically by the compiler. Were it not, you would need to call ToList<B>().

这篇关于不能含蓄转化类型System.Collections.Generic.IEnumerable&LT; B&GT;到System.Collections.Generic.List&LT; B&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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