Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Processing a C# Dictionary using LINQ

Tags:

c#

linq

How do I write the "// Display using Foreach" loop implementation using LINQ Lambda Expression / Statemene Expression?

I want to simplify my development and avoid nested foreach loops as much as possible. I am trying to include more logic with in the second foreach statement and I want to use Lambda / Statement expression.

 internal class Program
{
    internal class Country
    {
        public string CountryName { get; set; }
        public int CountryCode { get; set; }
    }
    static void Main(string[] args)
    {
        List<Country> countries = new List<Country>()
        {
            new Country{CountryName = "India", CountryCode=1},
            new Country{CountryName = "Andaman Nicobar", CountryCode=1},
            new Country{CountryName = "United States of America",  CountryCode=2},
            new Country{CountryName = "Alaska",  CountryCode=2},
            new Country{CountryName = "Hawaii",  CountryCode=2},
            new Country{CountryName = "United Kingdom", CountryCode=3},
            new Country{CountryName = "Australia", CountryCode=4}
        };

        Dictionary<int, List<Country>> countriesDictionary = new Dictionary<int, List<Country>>();
        foreach (Country country in countries)
        {
            if (!countriesDictionary.ContainsKey(country.CountryCode))
                countriesDictionary.Add(country.CountryCode, new List<Country>());
            countriesDictionary[country.CountryCode].Add(country);                
        }

       // Display using Foreach

        foreach (int key in countriesDictionary.Keys)
        {                             
            List<Country> dCountries = countriesDictionary[key];

            foreach (Country country in dCountries)
            {
                if (country.CountryCode.Equals(key))
                {
                    Console.WriteLine(country.CountryName);
                }                
            }
            Console.WriteLine();            
        }
        Console.ReadLine();
    }

Please suggest.

like image 331
Sriram B Avatar asked Jul 05 '13 13:07

Sriram B


People also ask

What is Processing used for?

Processing is a simple programming environment that was created to make it easier to develop visually oriented applications with an emphasis on animation and providing users with instant feedback through interaction. The developers wanted a means to “sketch” ideas in code.

What does Processing mean in programming?

Processing describes software manipulating or extracting data from a stored file. 3. Processing is a graphics library and IDE based on the Java programming language, first released in 2001 by developers Casey Reas and Ben Fry.

What is Processing library?

Processing is an open-source low level animation and GUI library built on Java with additional simplifications like additional classes, aliased mathematical functions and operations. It also provides a GUI for simple compilation of the programs written in processing.

Is Processing a framework?

js is a discontinued JavaScript port of Processing, a framework designed to write visualisations, images, and interactive content. It allows web browsers to display animations, visual applications, games and other graphical rich content without the need for a Java applet or Flash plugin.


2 Answers

This is another alternative:

countriesDictionary.ToList().ForEach
(
    pair =>
    {
        pair.Value.ForEach(country => Console.WriteLine(country.CountryName));
        Console.WriteLine();
    }
);

Also, this one based on Romoku's Answer(the answer was removed):

var countriesDictionary = countries.ToLookup(x => x.CountryCode, x => x);

foreach(var countryGroup in countriesDictionary)
{
    foreach(var country in countryGroup)
    {
        Console.WriteLine(country.CountryName);
    }
    Console.WriteLine();
}
like image 162
Theraot Avatar answered Oct 23 '22 01:10

Theraot


If you want to group countries by code, then you don't need two dictionaries. Use Enumerable.GroupBy

foreach(var codeGroup in countries.GroupBy(c => c.CountryCode))
{
    foreach(var country in codeGroup)
       Console.WriteLine(country.CountryName);

    Console.WriteLine();
}

Or just use your countriesDictionary (it already has countries grouped by code):

foreach(var kvp in countriesDictionary)
{
    foreach(var country in kvp.Value)
       Console.WriteLine(country.CountryName);

    Console.WriteLine();
}
like image 6
Sergey Berezovskiy Avatar answered Oct 22 '22 23:10

Sergey Berezovskiy