Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
SQL Server 119
#SQLServer 88
Oracle 70
#SQL SERVER 35
BRH 31
SQL Server 2012 29
denali 23
#TSQL 19
TSQL 19
C# 15

Archive · View All
October 2011 31
November 2011 30
September 2011 30
August 2011 18
December 2011 15
July 2011 13
June 2011 8
May 2012 4
April 2012 3
January 2010 3

Dictionary pivoting in C#

May 11 2012 12:00AM by Niladri Biswas   

Given a dictionary object as under

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

We need to transpose the rows to columns.

Approach 1:

Dictionary&tt;string, List<string>> dictContinents = new Dictionary&tt;string, List<string>>();
dictContinents.Add("Asia", new List() { "India", "Pakistan", "Bangaladesh", "Singapore" });
dictContinents.Add("Africa", new List() { "Uganda", "Ghana", "Ecuador","Somalia" });
dictContinents.Add("Europe", new List() { "France", "Norway", "Belgium", "Spain" });
dictContinents.Add("South America", new List() { "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&tt;string, List<string>> dictContinents = new Dictionary&tt;string, List<string>>();
dictContinents.Add("Asia", new List() { "India", "Pakistan", "Bangaladesh", "Singapore" });
dictContinents.Add("Africa", new List() { "Uganda", "Ghana", "Ecuador","Somalia" });
dictContinents.Add("Europe", new List() { "France", "Norway", "Belgium", "Spain" });
dictContinents.Add("South America", new List() { "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 will be helpful.

Tags: Dictionary,C#,Pivoting


Niladri Biswas
7 · 21% · 6720
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

1  Comments  

  • Hi biswas, adding continent names in output columns makes it more understandable. . I doubt whether pivoting in db gives more perf? I am not sure..

    commented on May 30 2012 10:44AM
    Ramireddy
    3 · 40% · 12972

Your Comment


Sign Up or Login to post a comment.

"Dictionary pivoting in C#" rated 5 out of 5 by 2 readers
Dictionary pivoting in C# , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]