在 LINQ 中将父子组合到一个列表中?

问题描述 投票:0回答:4

我有

public class Parent 
{
    public string Region { get; set; }
    public string PartOfRegion { get; set; }
    public string Type { get; set; }
    public List<Child> Childs { get; set; }
}

public class Child
{  
    public int Year { get; set; }
    public int Value { get; set; }
}

List<Parent> listParent { get; set; } // This list is feed with alot of data and child data to every parent

我想要这个

public class Result
{
    public string Region { get; set;}
    public string PartOfRegion { get; set; }
    public string Type { get; set; }
    public int Year { get; set; }
    public int Value { get; set; }
}

List<Result> resultList = listParent.[LINQ]??

所以我可以根据地区或 PartOfregion 和年份等对价值求和。

我不知道如何在 C# 中使用 LINQ 制作这个结果表。

谁能给我一个方向?

c# linq parent-child
4个回答
0
投票

您应该使用

SelectMany
从父级生成所有组合。

示例代码:

using System;


List<Parent> listParent = new List<Parent> { new Parent { Region = "a",
    Children = new List<Child> { new Child() { Value = "va" }, new Child() { Value = "vaa" } } }, new Parent { Region = "b",
    Children = new List<Child> { new Child() { Value = "vb" }, new Child() { Value = "vbb" } } }};


List<Result> results = listParent.SelectMany(o=> o.Children.Select(c=> new Result { Value = c.Value, Region = o.Region })).ToList();

Console.WriteLine("");
public class Parent
{
    public string Region { get; set; }
    public string PartOfRegion { get; set; }
    public string Type { get; set; }
    public List<Child> Children { get; set; }
}

public class Child
{
    public int Year { get; set; }
    public string Value { get; set; }
}

public class Result
{
    public string Region { get; set; }
    public string PartOfRegion { get; set; }
    public string Type { get; set; }
    public int Year { get; set; }
    public string Value { get; set; }
}

0
投票

要将列表转换为列表,您可以使用 LINQs SelectMany 运算符。您可以使用这样的代码:

List<Result> resultList = listParent.SelectMany(parent =>
    parent.Childs.Select(child =>
        new Result {
            Region = parent.Region,
            PartOfRegion = parent.PartOfRegion,
            Type = parent.Type,
            Year = child.Year,
            Value = child.Value
        }
    )
).ToList();

0
投票

你通常会写一个 LINQ 语句来做你需要做的任何事情,但查询将取决于你到底需要什么。

例如,如果您想要按区域划分的每个孩子的值的总和

listParent.GroupBy(p => p.Region)
  .Select(group => new {
      Region = group.Key, 
      Sum = group.SelectMany(p => p.Childs).Sum(c => c.Value)});

但总体方法是采用原始列表并应用 linq 运算符将其转换为我们想要的任何内容。可能很难确切地知道要使用什么运算符,但是 StackOverflow 上有很多关于最常见情况的问题。如果找不到任何相关内容,针对您的特定查询提出更具体的问题可能更有用。


0
投票

如果你想按

Region
PartOfRegion
Type
Year
(来自 Child)分组,首先你必须展平
listParent
,然后分组,然后创建
List<Result>

List<Result> resultList = listParent.SelectMany(parent => parent.Childs.Select(child => new
{
    Region = parent.Region,
    PartOfRegion = parent.PartOfRegion,
    Type = parent.Type,
    Year = child.Year,
    Value = child.Value
}))
.GroupBy(item => new { item.Region, item.PartOfRegion, item.Type, item.Year })
.Select(group => new Result()
{
    Region = group.Key.Region,
    PartOfRegion = group.Key.PartOfRegion,
    Type = group.Key.Type,
    Year = group.Key.Year,
    Value = group.Sum(item => item.Value)
})
.ToList();
© www.soinside.com 2019 - 2024. All rights reserved.