Storing 2 columns into a List

101,976

Solution 1

you can also make 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");

Solution 2

You create a class that will represent a row with 2 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 you store in a List<Foo>:

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

Solution 3

Use a tuple struct like KeyValuePair

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

Solution 4

I would use a class

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

 public class MyDataClass
 {
     public string Value1 { get; set; }
     public string Value2 { get; set; }
 }

Solution 5

You can either create a new class to hold the data, Or you could 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 ID of some sort, you could also consider using a Dictionary<>.

Share:
101,976
Cocoa Dev
Author by

Cocoa Dev

Updated on July 09, 2021

Comments

  • Cocoa Dev
    Cocoa Dev almost 3 years

    How can I store data from 2 columns (from a database) in a List

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

    Any help is appreciated

  • Cocoa Dev
    Cocoa Dev over 12 years
    Can you show me how you would get/set the data using your class. SO far it looks like a great start.
  • Cocoa Dev
    Cocoa Dev over 12 years
    how would I get the data from my new class?
  • Darin Dimitrov
    Darin Dimitrov over 12 years
    @CocoaDev, this will greatly depend on how is your data stored in the database, what database access technology are you using, if you are using an ORM tool or not, ... There are like gazillions of possibilities here. If have no clue where to start, ADO.NET is the de-facto database access technology in .NET that every developer doing relational database programming should start with when learning relational data access.
  • Cocoa Dev
    Cocoa Dev over 12 years
    I am getting the data from an Excel spreadsheet then storing it into a List (the example you showed me). So basically at this point. My data is being held in a variable called _items3
  • Darin Dimitrov
    Darin Dimitrov over 12 years
    @CocoaDev, OK, so what is your question? How are you retrieving the data from this Excel spreadsheet or is it what you are asking?
  • Cocoa Dev
    Cocoa Dev over 12 years
    My existing code (will need to be updated for 2nd column) was Listbox1.DataSource = _items3. This is when items3 was a List<string>. Now items3 is a List<Foo>. So how would I get the data from the items3 to appear as Text in the Listbox1.DataSource
  • Darin Dimitrov
    Darin Dimitrov over 12 years
    @CocoaDev, you could set the DataTextField and the DataValueField properties of the ListBox (assuming you are talking about an ASP.NET ListBox control) to the corresponding property names. For example: ListBox1.DataTextField = "Column1";. If you are doing WinForm development the two properties that you are looking for are called DisplayMember and ValueMember.
  • Cocoa Dev
    Cocoa Dev over 12 years
    I dont think it works like that. ListBox1 is inside "public partial class Form1 : Form". How is ListBox1.DisplayMember = items3.Column1?
  • Cocoa Dev
    Cocoa Dev over 12 years
    I get this error Warning 1 The class Form1 can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again. 0 0
  • Fandango68
    Fandango68 over 8 years
    I don't see why this was the answer, then the question was about a List, not an Array of a List.
  • Leo Gurdian
    Leo Gurdian about 7 years
    @CocoaDev - this is old but I think you need to do a little more research unto what Darin is telling you.