Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq从列表中的多个列表中选择值_C#_Linq - Fatal编程技术网

C# Linq从列表中的多个列表中选择值

C# Linq从列表中的多个列表中选择值,c#,linq,C#,Linq,我有一个列表,其中包含IntPtr这样的变量 var testList = new List<List<IntPtr>>(); 我到底做错了什么?如何修改此语句以使其按预期工作?当前,您正在向SelectMany传递一个谓词,这不是它所期望的;相反,它期望的是一个Func,其中List是输入列表,IEnumerable是函数调用时返回的值 SelectMany然后将每个嵌套的IEnumerable折叠成一个IEnumerable 除此之外,您还尝试使用=运算符,该序列由

我有一个列表,其中包含
IntPtr
这样的变量

var testList = new List<List<IntPtr>>();

我到底做错了什么?如何修改此语句以使其按预期工作?

当前,您正在向
SelectMany
传递一个谓词,这不是它所期望的;相反,它期望的是一个
Func
,其中
List
是输入列表,
IEnumerable
是函数调用时返回的值

SelectMany
然后将每个嵌套的
IEnumerable
折叠成一个
IEnumerable

除此之外,您还尝试使用
=列表
)上的code>运算符,该序列由
指针
表示,该指针不起作用

相反,您应该首先通过
SelectMany
折叠嵌套序列,然后通过
Where
子句应用谓词:

var pointers = testList.Where(list => list.Count > 0) // IEnumerable<List<IntPtr>>
                       .SelectMany(list => list) // IEnumerable<IntPtr>
                       .Where(pointer => pointer != IntPtr.Zero); // IEnumerable<IntPtr>
var pointers = testList.Where(list => list.Count > 0) // IEnumerable<List<IntPtr>>
                       .SelectMany(list => list) // IEnumerable<IntPtr>
                       .Where(pointer => pointer != IntPtr.Zero); // IEnumerable<IntPtr>
var pointers = testList.SelectMany(list => list) // IEnumerable<IntPtr>
                       .Where(pointer => pointer != IntPtr.Zero); // IEnumerable<IntPtr>