Skip to content

Instantly share code, notes, and snippets.

@booyaa
Created October 4, 2013 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save booyaa/6826721 to your computer and use it in GitHub Desktop.
Save booyaa/6826721 to your computer and use it in GitHub Desktop.
C# List of Lists

Don't forget to add the following referencs to your query if using LINQPad:

System.Collections
System.Data.OleDb
System.Data.SqlClient
System.Xml
System.Xml.XPath
void Main()
{
	DataTable dt = new DataTable("Test");
	dt.Columns.Add(new DataColumn("key", typeof(System.String)));
	dt.Columns.Add(new DataColumn("value", typeof(System.String)));
	dt.Rows.Add("hello", "world");
	dt.Rows.Add("foo", "bar");
	dt.Rows.Add("fuck", "yeah");
	dt.Rows.Add("yogi", "booboo");
	
	
	Console.WriteLine("dt has {0} rows ", dt.Rows.Count);
	
	List<List<string>> foo = new List<List<string>>();			
	
	int columnsCount = dt.Columns.Count;
	
	for(int i = 0; i < columnsCount; i++) {
			foo.Add(new List<string>());
	}
	
	Console.WriteLine("dt has {0} rows. foo has {1} slots.", dt.Rows.Count, foo.Count);
	foreach (DataRow row in dt.Rows) {
		object[] rowArray = row.ItemArray;							
		for(int i = 0; i < row.ItemArray.Length; i++) {
			foo[i].Add(rowArray[i].ToString());
			Console.Write("|{0,2}-{1,-10}", i, rowArray[i].ToString());
		}
		Console.WriteLine("|");		
	}	
	Console.WriteLine("foo has {0} lists and each list is {1} deep. Dump:", foo.Count, foo[0].Count);
	Console.WriteLine(foo);
	
	foreach(List<string> bar in foo) {
		foreach(string line in bar) {
			Console.WriteLine(line);
		}
		Console.WriteLine("--------------[new list]=================---------");
	}
}

// Define other methods and classes here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment