How to populate List<string> with Datarow values from single columns...

Posted by James on Stack Overflow See other posts from Stack Overflow or by James
Published on 2010-04-12T16:56:53Z Indexed on 2010/04/12 17:13 UTC
Read the original article Hit count: 402

Filed under:
|
|
|
|

Hi, I'm still learning (baby steps). Messing about with a function and hoping to find a tidier way to deal with my datatables.

For the more commonly used tables throughout the life of the program, I'll dump them to datatables and query those instead. What I'm hoping to do is query the datatables for say column x = "this", and convert the values of column "y" directly to a List to return to the caller:

    private List<string> LookupColumnY(string hex)
    {
        List<string> stringlist = new List<string>();
        DataRow[] rows = tblDataTable.Select("Columnx = '" + hex + "'", "Columny ASC");
        foreach (DataRow row in rows) { stringlist.Add(row["Columny"].ToString()); } 
        return stringlist;
    }

Anyone know a slightly simpler method? I guess this is easy enough, but I'm wondering if I do enough of these if iterating via foreach loop won't be a performance hit. TIA!

© Stack Overflow or respective owner

Related posts about c#

Related posts about datatable