How do I retrieve specific column with values fro开发者_如何学Pythonm dataset table with x no of columns without using loop? Any Help?
dataset.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
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);
}
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.
精彩评论