我在从对象列表中选择一个byte []时遇到一些麻烦,模型被设置为:
public class container{
public byte[] image{ get;set; }
//some other irrelevant properties
}
Run Code Online (Sandbox Code Playgroud)
在我的控制器中我有:
public List<List<container>> containers; //gets filled out in the code
Run Code Online (Sandbox Code Playgroud)
我试图拉下image一个级别,List<List<byte[]>>所以到目前为止,我有一个使用LINQ:
var imageList = containers.Select(x => x.SelectMany(y => y.image));
Run Code Online (Sandbox Code Playgroud)
但它扔了:
cannot convert from
'System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<byte>>' to
'System.Collections.Generic.List<System.Collections.Generic.List<byte[]>>'
Run Code Online (Sandbox Code Playgroud)
显然它是选择字节数组作为一个字节?
一些指导将不胜感激!
Jon*_*eet 11
你不想要SelectMany的image属性-这是要给一个字节序列.对于每个容器列表,您希望将其转换为字节数组列表,即
innerList => innerList.Select(c => c.image).ToList()
Run Code Online (Sandbox Code Playgroud)
...然后你想将该投影应用到你的外部列表:
var imageList = containers.Select(innerList => innerList.Select(c => c.image)
.ToList())
.ToList();
Run Code Online (Sandbox Code Playgroud)
请注意ToList在每种情况下调用将IEnumerable<T>a 转换为a List<T>.