【问题标题】:Cannot convert from 'System.Collections.Generic.List<byte[]>' to 'System.Collections.Generic.IEnumerable<System.Collections.Generic.List<byte[]>> [duplicate]无法从 'System.Collections.Generic.List<byte[]>' 转换为 'System.Collections.Generic.IEnumerable<System.Collections.Generic.List<byte[]>> [重复]
【发布时间】:2021-01-25 16:46:46
【问题描述】:

我试图在没有参考的情况下克隆 List,但是当我尝试运行代码时出现此错误:

无法从 'System.Collections.Generic.List' 转换为 'System.Collections.Generic.IEnumerable>

这是我想要运行的代码:

newList = new List<List<byte[]>>(oldList).ToArray();

这是否有效,还是有其他解决方案?

【问题讨论】:

  • 我也这么认为:newList = (List)oldList.ToList();
  • List&lt;byte[]&gt; 不是List&lt;List&lt;byte[]&gt;&gt;。具体来说,您使用的List constructor 需要IEnumerable&lt;T&gt;,根据您的代码,在本例中为IEnumerable&lt;List&lt;byte[]&gt;&gt;。虽然我不确定为什么 newList = oldList.ToList(); 不适合这种情况。

标签: c#


【解决方案1】:

试试:

newList = new List<List<byte[]>>(oldList).ToList();

【讨论】:

  • .ToArray() 更改为.ToList() 并不会神奇地将oldList 变成IEnumerable&lt;List&lt;byte[]&gt;&gt;,因为这完全需要它才能工作。从错误消息来看,oldListList&lt;byte[]&gt;
猜你喜欢
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多