c# – 使用LINQ选择字节数组
发布时间:2020-12-15 23:44:44 所属栏目:百科 来源:网络整理
导读:我在从对象列表中选择一个byte []时遇到一些麻烦,模型被设置为: public class container{ public byte[] image{ get;set; } //some other irrelevant properties } 在我的控制器中我有: public ListListcontainer containers; //gets filled out in the co
我在从对象列表中选择一个byte []时遇到一些麻烦,模型被设置为:
public class container{ public byte[] image{ get;set; } //some other irrelevant properties } 在我的控制器中我有: public List<List<container>> containers; //gets filled out in the code 我试图将图像向下拉一级,所以我留下了一个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[]>>' 显然它是将字节数组选为字节? 一些指导将不胜感激! 解决方法
你不希望SelectMany用于图像属性 – 这将给出一个字节序列.对于每个容器列表,您希望将其转换为字节数组列表,即
innerList => innerList.Select(c => c.image).ToList() …然后你想将该投影应用到你的外部列表: var imageList = containers.Select(innerList => innerList.Select(c => c.image) .ToList()) .ToList(); 注意在每种情况下对ToList的调用以转换IEnumerable< T>.到列表< T>. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |