C# 使用LINQ选择字节数组
我在从对象列表中选择字节[]时遇到一些问题模型设置为:C# 使用LINQ选择字节数组,c#,linq,C#,Linq,我在从对象列表中选择字节[]时遇到一些问题模型设置为: public class container{ public byte[] image{ get;set; } //some other irrelevant properties } 在我的控制器中,我有: public List<List<container>> containers; //gets filled out in the code 但它是: cannot convert
public class container{
public byte[] image{ get;set; }
//some other irrelevant properties
}
在我的控制器中,我有:
public List<List<container>> containers; //gets filled out in the code
但它是:
cannot convert from
'System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<byte>>' to
'System.Collections.Generic.List<System.Collections.Generic.List<byte[]>>'
无法从转换
“System.Collections.Generic.IEnumerable”到
'System.Collections.Generic.List'
显然,它选择字节数组作为字节
如果您能提供一些指导,我们将不胜感激 您不希望
为图像
属性选择多个
——这将给出一个字节序列。对于每个容器列表,您希望将其转换为字节数组列表,即
innerList => innerList.Select(c => c.image).ToList()
。。。然后,您希望将该投影应用到外部列表:
请注意,在每种情况下都需要调用
ToList
,以将IEnumerable
转换为列表
。首先,遵循.NET命名约定是一个非常好的主意,例如容器
和图像
@Jon-很抱歉,如果您在代码中使用更好的名称,这些都是从我的代码中复制/粘贴的伪名称,那么请在你的例子中也使用更好的名字。任何非常规的东西都会破坏代码的可读性,尽管这段代码不会投入生产,但你仍然要求人们阅读它。啊,是的,我完全没有考虑过。我以后会记住的!谢谢你的帮助
innerList => innerList.Select(c => c.image).ToList()
var imageList = containers.Select(innerList => innerList.Select(c => c.image)
.ToList())
.ToList();