What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 2714 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > C# > Dictionary pivoting in C# ...
Niladri.Biswas

Dictionary pivoting in C#

 Code Snippet posted by: Niladri.Biswas | Posted on: 5/11/2012 | Category: C# Codes | Views: 848 | Status: [Member] | Points: 40 | Alert Moderator   


Given a dictionary object as under

 Dictionary<string, List<string>> dictContinents = new Dictionary<string, List<string>>();

dictStr.Add("Asia", new List<string>() { "India", "Pakistan", "Bangaladesh", "Singapore" });
dictStr.Add("Africa", new List<string>() { "Uganda", "Ghana","Ecuador" });
dictStr.Add("Europe", new List<string>() { "France", "Norway" , "Belgium" ,"Spain" });
dictStr.Add("South America", new List<string>() { "Argentina", "Bolivia" , "Brazil" ,"Uruguay", "Chile", "Colombia" });


We need to transpose the rows to columns.

Approach 1:

Dictionary<string, List<string>> dictContinents = new Dictionary<string, List<string>>();

dictContinents.Add("Asia", new List<string>() { "India", "Pakistan", "Bangaladesh", "Singapore" });
dictContinents.Add("Africa", new List<string>() { "Uganda", "Ghana", "Ecuador","Somalia" });
dictContinents.Add("Europe", new List<string>() { "France", "Norway", "Belgium", "Spain" });
dictContinents.Add("South America", new List<string>() { "Chile", "Bolivia", "Brazil", "Peru" });

Enumerable.Range(0, dictContinents.Values.First().Count)
.ToList()
.ForEach(i => Console.WriteLine(string.Join(" ", dictContinents.Values.Select(values => values))));


Approach 2:

Dictionary<string, List<string>> dictContinents = new Dictionary<string, List<string>>();

dictContinents.Add("Asia", new List<string>() { "India", "Pakistan", "Bangaladesh", "Singapore" });
dictContinents.Add("Africa", new List<string>() { "Uganda", "Ghana", "Ecuador","Somalia" });
dictContinents.Add("Europe", new List<string>() { "France", "Norway", "Belgium", "Spain" });
dictContinents.Add("South America", new List<string>() { "Chile", "Bolivia", "Brazil", "Peru" });

int maxLimit = dictContinents.Values.First().Count;
//or dictContinents.Values.Select(l => l.Count).Max();
//or dictContinents.Values.Count

for (int i = 0; i < maxLimit; i++)
{
StringBuilder sb = new StringBuilder();
string space = string.Empty;
dictContinents.Keys.ToList().ForEach(k =>
{
sb.AppendFormat("{0}{1}", space, dictContinents[k]);
space = " ";
});

Console.WriteLine(sb.ToString());
}


Output

India            Uganda    France  Chile

Pakistan Ghana Norway Bolivia
Bangaladesh Ecuador Belgium Brazil
Singapore Somalia Spain Peru



Hope this helps

Best Regards,
Niladri Biswas
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/22/2013 12:13:30 AM