Home > other >  add all values to a list of list from a list
add all values to a list of list from a list

Time:10-28

I have a list of type class abc

public class abc
{
    public int count;
    public string country;
}

The list can have values like

Count: 1 - country: US
Count: 2 - country: US
Count: 3 - country: IND
Count: 4 - country: UK
Count: 5 - country: UK

Now I want to put this list into a list of lists where it should be segregated based on countries. My new list of lists should look like this

Count: 1 - country: US
Count: 2 - country: US

Count: 3 - country: IND

Count: 4 - country: UK
Count: 5 - country: UK

The count can have any integer and the country can have any string.

Is there any easy way to do this?

CodePudding user response:

You can use GroupBy and select afterwards each group into a separate list:

List<abc> mylist = new List<abc>()
{
    new abc{count = 1, country = "US"},
    new abc{count = 2, country = "US"},
    new abc{count = 3, country = "IND"},
    new abc{count = 4, country = "UK"},
    new abc{count = 5, country = "UK"},
};

List<List<abc>> result = mylist.GroupBy(x => x.country).Select(y => y.ToList()).ToList();

this way you get a list containing 3 other lists

CodePudding user response:

Implement it like this:

        List<abc> list = new List<abc>()
        {
            new abc() {country = "US", count = 1},
            new abc() {country = "US", count = 2},
            new abc() {country = "IND", count = 3},
            new abc() {country = "UK", count = 4},
            new abc() {country = "UK", count = 5}
        };
        
        Dictionary<string,List<abc>> dictionary = new Dictionary<string, List<abc>>();
        foreach (var item in list)
        {
            if(!dictionary.TryGetValue(item.country,out var l))
            {
                l = new List<abc>();
                dictionary.Add(item.country,l);
            }
            l.Add(item);
        }

        List<List<abc>> result = dictionary.Values.ToList();

CodePudding user response:

you can do like this.

List<abc> ls = new List<abc>();
ls.Add(new abc() { count = 1, country = "US" });
ls.Add(new abc() { count = 2, country = "US" });
ls.Add(new abc() { count = 3, country = "IND" });
ls.Add(new abc() { count = 4, country = "UK" });
ls.Add(new abc() { count = 5, country = "UK" });


List<List<abc>> listOfList = new List<List<abc>>();

foreach (var group in ls.GroupBy(x => x.country))
{
    List<abc> list = new List<abc>();
    foreach (var item in group)
    {
        list.Add(new abc() { count = item.count, country = item.country });
    }

    listOfList.Add(list);
}

LINQ

List<List<abc>> listOfList = new List<List<abc>>();
foreach (var (group, list) in from item in ls.GroupBy(x => x.country)
                            let temp = new List<abc>()
                            select (item, temp))
{
    foreach (var item2 in group)
    {
        list.Add(new abc() { count = item2.count, country = item2.country });
    }

    listOfList.Add(list);
}

CodePudding user response:

Like many already answered, you could group your list by the contryString. However I personally would preffer to add it into a dictionary, so the access would be much easier to understand.

List<abc> myList = new List<abc>()
{
    new abc{count = 1, country = "US"},
    new abc{count = 2, country = "US"},
    new abc{count = 3, country = "IND"},
    new abc{count = 4, country = "UK"},
    new abc{count = 5, country = "UK"},
};

You could as mentioned just group them:

var grouppedLists = myList.GroupBy(x => x.country).Select(y => y.ToList()).ToList();

or you can make a dictionary out of it:

var myDictionary = myList.Select(item => item.country).Distinct().ToDictionary(country => country, country => myList.Where(item => item.country == country).ToList());

Now having the dictionary, you could access the specific list by the key (country). For example:

myDictionary["US"]; //would give you all items with the country "US"

It is up to you to chose whatever you would like to use. Just be aware that if you use the dictionary, you need to handle the possible keyNotFoundException

CodePudding user response:

The easiest way to do this is with Linq.

You can use .GroupBy() to create groupings based on a properties value - in this case the Country.

In this example the .Select() statement uses a named tuple to make the data a bit more readable.

using System.Collections.Generic;
using System.Linq;

var data = new List<Abc>()
{
    new()
    {
        Count = 1,
        Country = "UK"
    },
    new()
    {
        Count = 2,
        Country = "UK"
    },
    new()
    {
        Count = 3,
        Country = "US"
    }
};

var groupedData = data
    .GroupBy(x => x.Country)
    .Select(x => (Country: x.Key, Data: x.ToList()))
    .ToList();

A way to consume and use this list of lists would be like so:

foreach (var (country, groupData) in groupedData)
{
    var groupDataString = string.Join(" ", groupData.Select(x => x.Count));
    Console.WriteLine($"{country}: {groupDataString}");
}

Example output looks like:

UK: 1 2
US: 3
  • Related