Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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,我有一个包含其他对象列表的对象列表 List<Parent> Ps; List-Ps; 家长: class Parent { List<Childs> Cs; } 类父类 { 名单C; } 是否有可能使用Linq创建父母和孩子的元组列表 Tuple<Parent, Child> 元组 您可以使用: List parentChilds=Ps .SelectMany(p=>p.Cs.Select(c=>Tuple.Create(p,c))) .T

我有一个包含其他对象列表的对象列表

List<Parent> Ps;
List-Ps;
家长:

class Parent 
{
   List<Childs> Cs;
}
类父类
{
名单C;
}
是否有可能使用Linq创建父母和孩子的元组列表

Tuple<Parent, Child>
元组
您可以使用:

List parentChilds=Ps
.SelectMany(p=>p.Cs.Select(c=>Tuple.Create(p,c)))
.ToList();
这等于:

var pcQuery = from parent in Ps
              from child in parent.Cs
              select Tuple.Create(parent, child);
List<Tuple<Parent, Child>> parentChilds = pcQuery.ToList();
var pcQuery=来自Ps中的父级
从parent.Cs中的child
选择Tuple.Create(父、子);
List parentChilds=pcQuery.ToList();

感谢您提供理解和lambda语法
var pcQuery = from parent in Ps
              from child in parent.Cs
              select Tuple.Create(parent, child);
List<Tuple<Parent, Child>> parentChilds = pcQuery.ToList();
var tuples = from p in Ps from c in p.Cs select Tuple.Create(p, c);