简体   繁体   English

创建一个 <Key,List<Value> &gt;从linq查询

[英]Creating a <Key,List<Value>> from a linq query

I've got two models - Country: 我有两种型号-国家:

  public int id { get; set; }

  public string CountryName { get; set; }

and FlagColor: 和FlagColor:

    public int id { get; set; }

    public string Color { get; set; }

    public virtual Country Country { get; set; }

I am trying to create an object from a linq query that would output an object of 我正在尝试从linq查询创建一个对象,该查询将输出一个

< CountryName,< List < ColorOfFlag>> <CountryName,<列表<ColorOfFlag >>

currently at the moment I have 目前我有

            var combinedList = from c in countries
            join fc in flagColors on c.id equals fc.Country.id
            select new {c, fc};

This is almost there but it is returning a row per flagColor so eg 这几乎到了,但是每个flagColor返回一行,因此例如

Belgium: Red 比利时:红色

Belgium: Black and so forth 比利时:黑色等等

how do I convert my query to out put: 如何将查询转换为输出:

Belguim: { Red, Black, Yellow} 贝尔吉姆(Belguim):{红色,黑色,黄色}

Hint: In LINQ you hardly ever need to use join, most of the time it is implicit via relations. 提示:在LINQ中,您几乎不需要使用联接,大多数情况下它是通过关系隐式的。

var combinedList = from fc in flagColors
                   group fc by fc.Country into g
                   select new
                   {
                       Country = g.Key.CountryName,
                       FlagColors = string.Join(",", g.Select(x => x.Color))
                   };

Note: I made it as an easy to read comma separated value. 注意:我将其作为易于阅读的逗号分隔值。 For List, you could do: 对于列表,您可以执行以下操作:

var combinedList = from fc in flagColors
                       group fc by fc.Country into g
                       select new
                       {
                           Country = g.Key.CountryName,
                           FlagColors = g.Select(x => x.Color).ToList()
                       };

Or if you meant to get a Dictionary: 或者,如果您想获取字典:

var combinedList = (from fc in flagColors
                   group fc by fc.Country into g
                   select new
                   {
                       Country = g.Key.CountryName,
                       FlagColors = g.Select(x => x.Color).ToList()
                   }).ToDictionary(l => l.Country, l=>l.FlagColors);

Shorter: 更短:

var combinedList = flagColors
                   .GroupBy(fc => fc.Country)
                   .ToDictionary(
                       g => g.Key.CountryName, 
                       g=>g.Select(c => c.Color).ToList()
                    );

Use Queryable.GroupBy (or corresponding Enumerable.GrouBy) 使用Queryable.GroupBy (或相应的Enumerable.GrouBy)

First group all your flag colors into groups where all flag colours have the same countryName.. Then from every group create one object, with the country Name and the list of all flag colors in this group: 首先将所有标记颜色归为一组,其中所有标记颜色均具有相同的countryName ..然后从每个组中创建一个对象,其中包含国家/地区名称和该组中所有标记颜色的列表:

var result = flagColors
    .GroupBy(flagColor => flagColor.Country.CountryName)
    .Select(group => new
    {
        CountryName = group.Key,                         // get common CountryName
        FlagColors = group                               // get the color of all elements
            .Select(groupElement => groupElement.Color)  // in the group
            .ToList(),
    })

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

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