gpt4 book ai didi

c# - 使用 LINQ 选择字节数组

转载 作者:行者123 更新时间:2023-11-30 13:30:47 25 4
gpt4 key购买 nike

我在从对象模型列表中选择 byte[] 时遇到了一些问题,模型设置为:

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

在我的 Controller 中我有:

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

我正在尝试拉 image下一级,所以我剩下一个 List<List<byte[]>>使用 LINQ到目前为止我有:

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[]>>'

貌似是把byte数组当成一个byte来选?

一些指导将不胜感激!

最佳答案

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

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

...然后您想将该投影应用于您的外部列表:

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

注意对 ToList 的调用在每种情况下转换 IEnumerable<T>List<T> .

关于c# - 使用 LINQ 选择字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27113258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com