linq C#嵌套列表中的唯一项

zdwk9cvp  于 2023-05-20  发布在  C#
关注(0)|答案(1)|浏览(118)

我有一个像下面的模型

public class Path
{
  public string Name {get; set;}
  public IEnumerable<Path> Items {get; set;}
}

& List<Path>中单个项目的列表数据表示形式,如下所示

new List<path>
{
  new Path
  {
    Name = "FirstRoot",
    Items = new List<Path>
    {
      new Path
      {
        Name = "2023",
        Items = new List<Path>
        {
          new Path
          {
            Name = "01",
            Items = new List<Path>
            {
              new Path
              {
                Name = "01"
                Items = new List<Path>
                {
                  new Path
                  {
                    Name = "01.text"
                    Items = null,
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  new Path
  {
    Name = "FirstRoot",
    Items = new List<Path>
    {
      new Path
      {
        Name = "2023",
        Items = new List<Path>
        {
          new Path
          {
            Name = "01",
            Items = new List<Path>
            {
              new Path
              {
                Name = "01"
                Items = new List<Path>
                {
                  new Path
                  {
                    Name = "02.text"
                    Items = null,
                  }
                }
              }
            }
          }
        }
      }
    }
  },
...
}
- FirstRoot - 2023 - 01 - 01.text
- FirstRoot - 2023 - 01 - 02.text
- SecondRoot - 2023 - 01 - 01.text
- SecondRoot - 2023 - 01 - 02.text
- ThirdRoot - 2023 - 01.text
- ThirdRoot - 2023 - 02.text

现在我如何从中得到唯一路径,使我的列表看起来像

new List<path>
{
  new Path
  {
    Name = "FirstRoot",
    Items = new List<Path>
    {
      new Path
      {
        Name = "2023",
        Items = new List<Path>
        {
          new Path
          {
            Name = "01",
            Items = new List<Path>
            {
              new Path
              {
                Name = "01"
                Items = new List<Path>
                {
                  new Path
                  {
                    Name = "01.text"
                    Items = null,
                  },
                  new Path
                  {
                    Name = "02.text"
                    Items = null,
                  },
                }
              }
            }
          }
        }
      }
    }
  },
  new Path
  {
    Name = "SecondRoot",
    Items = new List<Path>
    {
      new Path
      {
        Name = "2023",
        Items = new List<Path>
        {
          new Path
          {
            Name = "01",
            Items = new List<Path>
            {
              new Path
              {
                Name = "01"
                Items = new List<Path>
                {
                  new Path
                  {
                    Name = "01.text"
                    Items = null,
                  },
                  new Path
                  {
                    Name = "02.text"
                    Items = null,
                  },
                }
              }
            }
          }
        }
      }
    }
  },
...
}

我试着看看this解决方案,但找不到一个。

bxjv4tth

bxjv4tth1#

我设法得到了一个工作代码,张贴在这里,如果它帮助任何人。

private IEnumerable<Path> CleanPaths(IEnumerable<Path> items)
        {
            IEnumerable<Path> result = Enumerable.Empty<Path>();
            List<Path> distinctItems = new List<Path>();
            var distincts = items.Select(i => i.Name).Distinct().ToArray();

            foreach (var item in distincts)
            {
                var childs = items
                    .Where(j => j.Name == item && j.Items != null)
                    .SelectMany(k => k.Items)
                    .ToList();

                distinctItems.Add(new Path
                {
                    Name = item,
                    Items = CleanPaths(childs),
                });
            }

            result = distinctItems;

            return result;
        }

相关问题