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
Linq 如何将分层列表转换为平面列表?_Linq - Fatal编程技术网

Linq 如何将分层列表转换为平面列表?

Linq 如何将分层列表转换为平面列表?,linq,Linq,通常我们将平面列表转换为层次列表,但在我的例子中,我想将层次列表转换为平面列表 我有一个分层的列表,我想把这个分层列表转换成平面的列表类 假设我有一个下面的层次列表 Parent1 --- Child1 ,F1 ----Child2, F2 ----Child2, F3 Parent2 ----Child2,F2 ----Child4,F6 Parent3 --- Child1 ,F1 我需要如下输出: Parent1, Child1,F1 Parent1,

通常我们将平面列表转换为层次列表,但在我的例子中,我想将层次列表转换为平面列表

我有一个分层的
列表
,我想把这个分层列表转换成平面的
列表

假设我有一个下面的层次列表

 Parent1
  --- Child1 ,F1
  ----Child2, F2
  ----Child2, F3
 Parent2
  ----Child2,F2
  ----Child4,F6
  Parent3
  --- Child1 ,F1
我需要如下输出:

 Parent1, Child1,F1
 Parent1, Child2,F2
 Parent1, Child2,F3
 Parent2,Child2,F2
 Parent2,Child4, F6
 Parent3, Child1,F1

它可能不是最优化的解决方案(我认为它可以进一步最小化,并且我有一个更简洁的版本-但是这种形式是我使用最多的)

这是我一直在用的

public static IEnumerable<T> FlattenHierarchy<T>(this T node,
                                Func<T, IEnumerable<T>> getChildEnumerator)
{
    yield return node;
    if (getChildEnumerator(node) != null)
    {
        foreach (var child in getChildEnumerator(node))
        {
            foreach (var childOrDescendant
                        in child.FlattenHierarchy(getChildEnumerator))
            {
                yield return childOrDescendant;
            }
        }
    }
}

它可能不是最优化的解决方案(我认为它可以进一步最小化,并且我有一个更简洁的版本-但是这种形式是我使用最多的)

这是我一直在用的

public static IEnumerable<T> FlattenHierarchy<T>(this T node,
                                Func<T, IEnumerable<T>> getChildEnumerator)
{
    yield return node;
    if (getChildEnumerator(node) != null)
    {
        foreach (var child in getChildEnumerator(node))
        {
            foreach (var childOrDescendant
                        in child.FlattenHierarchy(getChildEnumerator))
            {
                yield return childOrDescendant;
            }
        }
    }
}

最后我得到了解决方案:

public class RNode 
        {
            public string Id;
            public long ID;
            public string name;
            public string subTitle;
            public IList<RNode> children;          
        } 
          public class FlatObj //For Data format
        {
            public long Id;
            public long ParentId;
            public long Position;
            public string name;
            public string subTitle;
        }
        List<FlatObj> GlobalFlatObj = new List<FlatObj>();
        List<long> ParentIdList = new List<long>();
        long CurrentParentId=0;
        long CurrentPosition = 0;

    public List<FlatObj> FlatData(IList<RNode> HData) //Converting Heirarchical to Flat
    {
        foreach (RNode node in HData)
        {
            FlatObj ObjFlatObj = new FlatObj();
            ObjFlatObj.Id = node.ID;
            ObjFlatObj.name = node.name;
            ObjFlatObj.ParentId = CurrentParentId;
            ObjFlatObj.Position = CurrentPosition;
            GlobalFlatObj.Add(ObjFlatObj);
            if (node.children.Count > 0)
            {
                CurrentParentId = node.ID;
                ParentIdList.Add(node.ID);
                FlatData(node.children);
            }
            CurrentPosition++;
        }
        if (ParentIdList.Count > 0)
        {
            ParentIdList.RemoveAt(ParentIdList.Count - 1);
            if (ParentIdList.Count > 0)
                CurrentParentId = ParentIdList[ParentIdList.Count - 1];
            CurrentPosition = 0;
        }
        return GlobalFlatObj;
    }
    public dynamic Test(List<RNode> EmployeeHierarchy)
    {
        var HierarchyResult = FlatData(EmployeeHierarchy); //Calling
        return Ok(HierarchyResult);
    }
输出:


]

我终于找到了解决方案:

public class RNode 
        {
            public string Id;
            public long ID;
            public string name;
            public string subTitle;
            public IList<RNode> children;          
        } 
          public class FlatObj //For Data format
        {
            public long Id;
            public long ParentId;
            public long Position;
            public string name;
            public string subTitle;
        }
        List<FlatObj> GlobalFlatObj = new List<FlatObj>();
        List<long> ParentIdList = new List<long>();
        long CurrentParentId=0;
        long CurrentPosition = 0;

    public List<FlatObj> FlatData(IList<RNode> HData) //Converting Heirarchical to Flat
    {
        foreach (RNode node in HData)
        {
            FlatObj ObjFlatObj = new FlatObj();
            ObjFlatObj.Id = node.ID;
            ObjFlatObj.name = node.name;
            ObjFlatObj.ParentId = CurrentParentId;
            ObjFlatObj.Position = CurrentPosition;
            GlobalFlatObj.Add(ObjFlatObj);
            if (node.children.Count > 0)
            {
                CurrentParentId = node.ID;
                ParentIdList.Add(node.ID);
                FlatData(node.children);
            }
            CurrentPosition++;
        }
        if (ParentIdList.Count > 0)
        {
            ParentIdList.RemoveAt(ParentIdList.Count - 1);
            if (ParentIdList.Count > 0)
                CurrentParentId = ParentIdList[ParentIdList.Count - 1];
            CurrentPosition = 0;
        }
        return GlobalFlatObj;
    }
    public dynamic Test(List<RNode> EmployeeHierarchy)
    {
        var HierarchyResult = FlatData(EmployeeHierarchy); //Calling
        return Ok(HierarchyResult);
    }
输出:


]

我们需要查看实际的类结构才能给出有意义的答案,但我会尝试使用
SelectMany
,当您陷入困境时再回来。Hi d Stanley的可能重复,我刚才给出的示例仅供您理解,我有非常bis的类结构,我无法提供beacz类非常庞大。正如你提到的使用selectmany我怎么能做到这一点。你能用一些代码举例说明吗。我们需要看看实际的类结构才能给出有意义的答案,但我会尝试使用
selectmany
,当你遇到困难时再回来。Hi d Stanley的可能副本,我刚刚给出了上面的示例,以供你理解,我有非常bis类结构,我不能提供beacz类是非常巨大的。正如你提到的使用selectmany我怎么能做到这一点..你能给我一些代码和例子吗。
[
{
    "$id": "1",
    "Id": 1,
    "ParentId": 0,
    "Position": 0,
    "name": "root",
    "subTitle": null
},
{
    "$id": "2",
    "Id": 2,
    "ParentId": 1,
    "Position": 0,
    "name": "child 1",
    "subTitle": null
},
{
    "$id": "3",
    "Id": 5,
    "ParentId": 2,
    "Position": 0,
    "name": "grandchild",
    "subTitle": null
},
{
    "$id": "4",
    "Id": 3,
    "ParentId": 1,
    "Position": 1,
    "name": "child 2",
    "subTitle": null
},
{
    "$id": "5",
    "Id": 4,
    "ParentId": 1,
    "Position": 2,
    "name": "child 3",
    "subTitle": null
}