使用 LINQ 选择字节数组



我在从对象列表内部选择字节[]时遇到一些问题 模型设置为:

public class container{
    public byte[] image{ get;set; }
    //some other irrelevant properties    
}

在我的控制器中,我有:

public List<List<container>> containers; //gets filled out in the code

我正在尝试将image拉低一个级别,因此我只能使用 LINQ List<List<byte[]>>到目前为止,我有:

var imageList = containers.Select(x => x.SelectMany(y => y.image));

但它正在抛出:

cannot convert from 
'System.Collections.Generic.IEnumerable<System.Collections.Generic.IEnumerable<byte>>' to 
'System.Collections.Generic.List<System.Collections.Generic.List<byte[]>>'  

显然它正在选择字节数组作为字节?

一些指导将不胜感激!

你不希望SelectMany image属性 - 这将给出一个字节序列。对于每个容器列表,您希望将其转换为字节数组列表,即

innerList => innerList.Select(c => c.image).ToList()

。然后,您希望将该投影应用于外部列表:

var imageList = containers.Select(innerList => innerList.Select(c => c.image)
                                                        .ToList())
                          .ToList();

请注意在每种情况下调用ToListIEnumerable<T>转换为List<T>

最新更新