Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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读取多达N级的列表,并将所需数据保存到另一个列表中_C#_Linq_List_Tree - Fatal编程技术网

C# 使用Linq读取多达N级的列表,并将所需数据保存到另一个列表中

C# 使用Linq读取多达N级的列表,并将所需数据保存到另一个列表中,c#,linq,list,tree,C#,Linq,List,Tree,我有一个第n级子对象的列表。我希望遍历该列表,并使用Linq将所需数据获取到另一个具有不同结构的列表 public class Node { public List<Node> Children = new List<Node>(); public Node Parent { get; set; } public FolderReportItem AssociatedObject { get; set; } } 公共类节点 { public Li

我有一个第n级子对象的列表。我希望遍历该列表,并使用Linq将所需数据获取到另一个具有不同结构的列表

public class Node
{
    public List<Node> Children = new List<Node>();
    public Node Parent { get; set; }
    public FolderReportItem AssociatedObject { get; set; }
}
公共类节点
{
public List Children=new List();
公共节点父节点{get;set;}
公共FolderReportItem关联对象{get;set;}
}
我有一个包含数据的IEnumerable列表。

子级达到n级的节点列表

我正在使用Linq创建具有Linq数据的新对象

下面是我如何创建新对象的代码

var jsonTree = new List<object>();

foreach (var node in nodesList)
{
    jsonTree.Add(new
    {
        id = node.AssociatedObject.ID,
        name = node.AssociatedObject.Name,
        children = node.Children.Select(p => new
        {
            id = p.AssociatedObject.ID,
            name = p.AssociatedObject.Name,
            children = p.Children.Select(q => new
            {
                id = q.AssociatedObject.ID,
                name = q.AssociatedObject.Name
            })
        })
    });
}
var jsonTree=newlist();
foreach(节点列表中的var节点)
{
jsonTree.Add(新的
{
id=node.AssociatedObject.id,
name=node.AssociatedObject.name,
children=node.children.Select(p=>new
{
id=p.AssociatedObject.id,
name=p.AssociatedObject.name,
children=p.children.选择(q=>new
{
id=q.AssociatedObject.id,
name=q.AssociatedObject.name
})
})
});
}

它并没有给我第n层的数据,因为它缺少读取数据的递归方法。如何将此转换为递归方法,或者是否有其他方法来实现此操作。

我相信这将满足您的要求。在递归调用函数之前,必须先声明函数

// Declare the function so that it can be referenced from within
// the function definition.
Func<Node, object> convert = null;

// Define the function.
// Note the recursive call when setting the 'Children' property.
convert = n => new 
{
    id = n.AssociatedObject.ID,
    name = n.AssociatedObject.Name,
    children = n.Children.Select(convert)
};

// Convert the list of nodes to a list of the new type.
var jsonTree = 
    nodes
    .Select(convert)
    .ToList();

你是说怎么做?我看到了这篇文章,但我真的不想用它stack@Muhammadzubair-您的代码是否不起作用?输出应该是什么样子的?我的代码正在工作,但读不到第n级。它读到了第三级。我需要读到第n级。
// Declare and define the function as you normally would.
object convert (Node node)
{
    id = n.AssociatedObject.ID,
    name = n.AssociatedObject.Name,
    children = n.Children.Select(convert);
};

// Convert the list of nodes to a list of the new type.
var jsonTree = 
    nodes
    .Select(convert)
    .ToList();