简体   繁体   English

将父子结构转换为子父级

[英]Transform a parent child structure to child parent

I am working on a project where I need to be able to transform a complex parent child structure to its child parent equivalent, this is necessary as the querying and reporting on the data needs to be possible from either the parent or child pov.我正在开发一个项目,我需要能够将复杂的父子结构转换为其子父等效项,这是必要的,因为需要可以从父或子 pov 查询和报告数据。 This may not make much sense, but here is a simplified version with some sample data:这可能没有多大意义,但这是一个带有一些示例数据的简化版本:

Lets say I have this data:假设我有这个数据:

base colours (red, blue, green) accent colours (red, yellow, blue)基色(红、蓝、绿) 强调色(红、黄、蓝)

And I want to transform it to:我想将其转换为:

red (base colours, accent colours) blue (base colours, accent colours) green (base colours) yellow (accent colours)红色(基色、强调色) 蓝色(基色、强调色) 绿色(基色) 黄色(强调色)

It seemed like Linq would be ideal for this but I can't figure it out, I know I could do it a long hand way but it just seems clunky.看起来 Linq 是这个的理想选择,但我想不通,我知道我可以做到这一点,但它看起来很笨重。

So far I have this test class to work against:到目前为止,我有这个测试类可以使用:

class UsageType
{
    public string Usage { get; set; }

    public List<Colour> Colours = new List<Colour>();
}

class Colour
{
    public string Name { get; set; }
}

public class TestClass
{
    public TestClass()
    {
        var usageTypes = new List<UsageType>()
            {
                new UsageType()
                {
                    Usage = "base colours",
                    Colours = new List<Colour>()
                    {
                        new Colour() { Name = "Red"},
                        new Colour() { Name = "Blue"},
                        new Colour() { Name = "Green"}
                    }
                },
                new UsageType()
                {
                    Usage = "accent colours",
                    Colours = new List<Colour>()
                    {
                        new Colour() { Name = "Red"},
                        new Colour() { Name = "Blue"},
                        new Colour() { Name = "Yellow"}
                    }
                }
            };

        var allColours = from clr in usageTypes
                         .SelectMany(ut => ut.Colours, (clr, ut) => new { colour = ut.Name, usage = clr.Usage })
                         select new { clr.colour, clr.usage };
    }
}

What I'd like to know is how to get from my flattened list of colours (the allColours variable in my sample) to having it structured as per my question.我想知道的是如何从我的扁平颜色列表(我的示例中的 allColours 变量)按照我的问题对其进行结构化。 Any help on the best way to do this would be appreciated.任何有关执行此操作的最佳方法的帮助将不胜感激。

GroupBy color will basically give you the desired format: GroupBy颜色基本上会给你所需的格式:

var colourUsage =
    from ut in usageTypes
    from c in ut.Colours
    group ut.Usage by c.Name into g
    select new { colour = g.Key, usages = g.ToList() };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM