簡體   English   中英

LINQ 查詢以對父元素和子元素進行分組

[英]LINQ query to group parent and child elements

我目前正在開發一個 .NET 4.7 應用程序。 我需要用未排序的數據創建一個樹結構。

最終的樹結構如下所示:

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

一個 Parent LocationViewModel 可以有多個 ChildLocations。 每個 ChildLocation 本身又可以有多個 ChildLocations。

我需要從以下結構中對數據進行排序。 我的未排序數據是List<LinkParentChildViewModel> 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<Location> Locations ,其中包含所有位置。

然后我得到一個List<LinkParentChildViewModel> LinksParentChild ,條目都混淆了 - 因此 Parent 可以是孩子,而孩子可以是父母。

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<LocationViewModel> 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"
                            }
                        }
                    }
                }
            }
        }
    }
};

你可以試試遞歸

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM