Saving 2 columns to a list

How to store data from 2 columns (from a database) in a list

List<string> _items = new List<string>(); 

Any help is appreciated.

+7
source share
6 answers

you can also create an array of list

 List<string> [] list= new List<String> []; list[0]=new List<string>(); list[1]=new List<string>(); list[0].add("hello"); list[1].add("world"); 
0
source

You create a class that will represent a row with two columns:

 public class Foo { // obviously you find meaningful names of the 2 properties public string Column1 { get; set; } public string Column2 { get; set; } } 

and then save to List<Foo> :

 List<Foo> _items = new List<Foo>(); _items.Add(new Foo { Column1 = "bar", Column2 = "baz" }); 
+38
source

Use a tuple structure like KeyValuePair

 List<KeyValuePair<string, string>> _items = new List<KeyValuePair<string, string>>(); _items.Add(new KeyValuePair<string, string>(foo, bar)); 
+20
source

I would use a class

  List<MyDataClass> _items = new List<MyDataClass>(); public class MyDataClass { public string Value1 { get; set; } public string Value2 { get; set; } } 
+6
source

You can either create a new class to store data, or use the built-in Tuple<> class. http://msdn.microsoft.com/en-us/library/system.tuple.aspx

Also, if one of the columns contains a unique identifier of some type, you can also consider using Dictionary<> .

+3
source

How to get data from a list of two new columns

 List<ListTwoColumns> JobIDAndJobName = new List<ListTwoColumns>(); for (int index = 0; index < JobIDAndJobName.Count;index++) { ListTwoColumns List = JobIDAndJobName[index]; if (List.Text == this.cbJob.Text) { JobID = List.ID; } } 
+1
source

All Articles