-1

I've got two models - Country:

  public int id { get; set; }

  public string CountryName { get; set; }

and 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

< CountryName,< List < 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 e.g

Belgium: Red

Belgium: Black and so forth

how do I convert my query to out put:

Belguim: { Red, Black, Yellow}

Jane Doe
  • 61
  • 9
  • 1
    Possible duplicate of [Using Linq to group a list of objects into a new grouped list of list of objects](https://stackoverflow.com/questions/2697253/using-linq-to-group-a-list-of-objects-into-a-new-grouped-list-of-list-of-objects) – mjwills Jun 30 '18 at 12:04
  • Doesn't `Country` have a property `FlagColors`? – Gert Arnold Jun 30 '18 at 19:53

2 Answers2

1

Hint: In LINQ you hardly ever need to use join, most of the time it is implicit via relations.

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()
                    );
Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39
1

Use Queryable.GroupBy (or corresponding 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:

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(),
    })
Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116