How do I retrieve specific column with values from dataset table with x no of columns without using loop? Any Help?
views:
36answers:
5dataset.tables[i].Columns[j]
where i is the table indexer and j is the column indexer.
or for a specific ros and columns dataset.tables[i].Rows[r][j]
where r is the row index
I was going to put this in as a comment, but it ended up getting a bit too long, from comments to ZombieSheep's answer:
@Dee, is there a specific reason you cant write a loop to capture the values?
Just for performance sake. – Dee
Have you measured it and confirmed that looping over the rows is a performance bottle-neck for you? Beware of making premature optimisations that could actually end up making things slower.
Don't forget that the compiler writers are cleverer people than me, probably than you as well, and whatever you end up doing will probably end up being compiled down to the same MSIL in the end.
Chances are any "magic" method that you might use to do this would probably be using a loop behind the scenes to do the work anyway so you may as well implement a simple solution to pull out the data for one column using a loop.
How about this extension method? (But don't ask me about performance ;-))
public static DataTable GetOneColumn(this DataTable dataTable, string columnName)
{
DataView view = new DataView(dataTable);
return view.ToTable(false, columnName);
}
Thank you all for your comments.I was just trying to find some alternate way to perform operation.