【问题标题】:LINQ query to group parent and child elementsLINQ 查询对父元素和子元素进行分组
【发布时间】:2020-02-10 22:39:14
【问题描述】:

我目前正在开发一个 .NET 4.7 应用程序。我需要用未排序的数据创建一个树结构。

最终的树形结构如下所示:

public class LocationViewModel
{
    public int Id { get; set; }
    public string Code { get; set; }
    public List<LocationViewModel> ChildLocations { get; set; }
}

一个父 LocationViewModel 可以有多个 ChildLocation。每个 ChildLocation 本身又可以有多个 ChildLocation。

我需要从以下结构中对数据进行排序。我的未排序数据是List&lt;LinkParentChildViewModel&gt; LinksParentChild,如下所示:

public class LinkParentChildViewModel
{
    public Location Parent { get; set; }
    public LocationLink Child { get; set; }
}

public class Location
{
   public int Id { get; set; }
   public string Code { get; set; }
}

public class LocationLink
{
   public int ParentLocationId { get; set; }
   public int ChildLocationId { get; set; }
}

首先我有一个List&lt;Location&gt; Locations,其中包含所有位置。

然后我得到一个List&lt;LinkParentChildViewModel&gt; LinksParentChild,所有条目都混淆了——因此父母可以是孩子,孩子可以是父母。

var LinksParentChild = new List<LinkParentChildViewModel>
{
    new LinkParentChildViewModel
    {
        Parent = new Location
        {
            Id = 8,
            Code = "ParLoc1",
        },
        Child = new LocationLink
        {
            ChildLocationId = 4,
            ParentLocationId = null
        }
    },
    new LinkParentChildViewModel
    {
        Parent = new Location
        {
            Id = 4,
            Code = "Loc1",
        },
        Child = new LocationLink
        {
            ChildLocationId = 6,
            ParentLocationId = 8
        }
    },
    new LinkParentChildViewModel
    {
        Parent = new Location
        {
            Id = 6,
            Code = "ChildLoc1",
        },
        Child = new LocationLink
        {
            ChildLocationId = null,
            ParentLocationId = 4
        }
    },
    new LinkParentChildViewModel
    {
        Parent = new Location
        {
            Id = 10,
            Code = "LeftLoc1",
        },
        Child = new LocationLink
        {
            ChildLocationId = 11,
            ParentLocationId = 4
        }
    },
    new LinkParentChildViewModel
    {
        Parent = new Location
        {
            Id = 11,
            Code = "LeftChildLoc1",
        },
        Child = new LocationLink
        {
            ChildLocationId = null,
            ParentLocationId = 10
        }
    }
};

我需要编写一个 LINQ 查询来将我数据中的所有节点分组到 List&lt;LocationViewModel&gt; result

var result = LinksParentChild.GroupBy(x => x.Parent.Id).Select(x => new LocationViewModel
{
    Id = x.First().Parent.Id,
    Code = x.First().Parent.Code,
    ChildLocations = new List<LocationViewModel>
    {
        // ... I'm stuck unfortunately, somehow i need to query and group all locations
    }
}).ToList();

我试过了,但不幸的是我卡住了:

  • 我需要像树状结构一样选择所有位置

你知道如何解决这个问题吗?

非常感谢!

结果如下:

var result = new List<LocationViewModel>
{
    new LocationViewModel
    {
        Id = 8,
        Code = "ParLoc1",
        ChildLocations = new List<LocationViewModel>
        {
            new LocationViewModel
            {
                Id = 4,
                Code = "Loc1",
                ChildLocations = new List<LocationViewModel>
                {
                    new LocationViewModel
                    {
                        Id = 6,
                        Code = "ChildLoc1"
                    },
                    new LocationViewModel
                    {
                        Id = 10,
                        Code = "LeftLoc1",
                        ChildLocations = new List<LocationViewModel>
                        {
                            new LocationViewModel
                            {
                                Id = 11,
                                Code = "LeftChildLoc1"
                            }
                        }
                    }
                }
            }
        }
    }
};

【问题讨论】:

标签: c# linq


【解决方案1】:

你可以试试递归

public static List<LocationViewModel> GetHierarchy(List<LinkParentChildViewModel> linkParentChildViewModels, int parentId)
{
    return linkParentChildViewModels.Where(x => x.Parent.Id == parentId).Select(x => new LocationViewModel
    {
        Id = x.Parent.Id,
        Code = x.Parent.Code,
        ChildLocations = GetHierarchy(linkParentChildViewModels, x.Child.ChildLocationId)
    }).ToList();
}

从 Main 方法调用它

var result = GetHierarchy(LinksParentChild, 8);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-26
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    相关资源
    最近更新 更多