Eric Klaus
Eric Klaus

Reputation: 955

Aggregate list within list into one list

I have those those definition

ClassCountryCollection is defined as:

public class ClassCountryCollection : KeyedCollection<string, ClassCountry>
    {
        protected override IEnumerable<string> GetKey(ClassCountry i)
        {
            return i.GetKey(i.Key);
        }
    }

ClassCountry is defined as

public class ClassCountry : ...
{
public string Key { get; set; }
public ClassCityCollection ClassCityList { get; set; }
...
}

ClassCityCollection is defined as:

public class ClassCityCollection : KeyedCollection<string, ClassCity>
{
    protected override string GetKeyForItem(ClassCity i)
    {
        return i.Name;
    }
}

And, finally ClassCity is as:

public class ClassCity : ...
{
   public int Population { get; set; }
   public string Name { get; set; }
...
}

I have an object of type ClassCountryCollection country

1. Key=Spain, ClassCityCollection{{Population=55,Name=Barcelona},{Population=65,Name=Madrid}}
2. Key=Germany, ClassCityCollection{{Population=12,Name=Berlin},{Population=125,Name=Dresden}}

I want to create a list such that above thing becomes

1. {"Spain|Population=55,Name=Barcelona,Population=65,Name=Madrid"}
2. {"Germany|Population=12,Name=Berlin,Population=125,Name=Dresden"}

I tried

country.Select(x => new { x.ClassCityList.Select(w => string.Join(",",w.ToString().ToList())), x.Key }).ToList();

And then i got stuck, don't know how to continue. Maybe i create ToString() somewhere there to make it easier.

Upvotes: 0

Views: 84

Answers (1)

neelesh bodgal
neelesh bodgal

Reputation: 662

I assumed the class structure and return this code.

    namespace welcome
   {

class Program
{
    static void Main(string[] args)
    {

        ClassCountryCollection cc = new ClassCountryCollection();

        var c1 = new ClassCountry()
        {
            Key = "Spain",
            ClassCityList = new ClassCityCollection{
                new ClassCity{Name ="Barcelona", Population = 55},
                new ClassCity{Name ="Madrid", Population = 65},
            }
        };

        var c2 = new ClassCountry()
        {
            Key = "Germany",
            ClassCityList = new ClassCityCollection{
                new ClassCity{Name ="Berlin", Population = 12},
                new ClassCity{Name ="Dresden", Population = 125},
            }
        };

        cc.Add(c1);
        cc.Add(c2);

        var g = cc.Select(x => x.Key + "|" + x.ClassCityList.Select(x => nameof(x.Population) + "=" + x.Population + "," + nameof(x.Name) + "=" + x.Name).Aggregate((d, s) => d + "," + s));
        foreach (var item in g)
        {
            System.Console.WriteLine(item);
        }
        Console.ReadLine();
    }
}

public class ClassCountryCollection : KeyedCollection<string, ClassCountry>
{

    protected override string GetKeyForItem(ClassCountry item)
    {
        return item.Key;
    }

}
public class ClassCountry
{
    public string Key { get; set; }
    public ClassCityCollection ClassCityList { get; set; }
}

public class ClassCityCollection : KeyedCollection<string, ClassCity>
{
    protected override string GetKeyForItem(ClassCity i)
    {
        return i.Name;
    }
}


public class ClassCity
{
    public int Population { get; set; }
    public string Name { get; set; }

}
}

Code can be compiled and run to check the output.

Output is

  Spain|Population=55,Name=Barcelona,Population=65,Name=Madrid
  Germany|Population=12,Name=Berlin,Population=125,Name=Dresden

when ordered by key and then name output is

  Germany|Population=12,Name=Berlin,Population=125,Name=Dresden
  Spain|Population=55,Name=Barcelona,Population=65,Name=Madrid

Code need to modified as

  var g = cc.OrderBy(x=>x.Key).Select(x=>x.Key + "|" + x.ClassCityList.OrderBy(x=>x.Name).Select(x=>nameof(x.Population) +"=" + x.Population+","+nameof(x.Name) +"="+x.Name).Aggregate((d,s)=>d+","+s)); 

Upvotes: 2

Related Questions