A fast way to find a row that contains a specific object in a .NET DataTable
I have the following scenario:
Public Class MyType
{
Public string A{ get;set;}
Public string 开发者_运维百科B{ get;set;}
}
.....
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("myNode", typeof(MyType)));
What is the most efficient way to locate a row that holds a MyType where A = "specific value" other than doing a for each?
Datatable.rows has a find() method, however I've never been satisfied with the speed of it if you're dealing with a large datatable or many finds. If you're only dealing with 2 columns of data, have a look at using a hashtable instead. They are VASTLY faster when it comes to searching.
There is Select and find methods for datatable. May be you can use that.
I addition to what Jeff Bane proposed, consider filtering your data at the database level (SELECT ... WHERE ...). For truly gargantuan volumes of data, nothing beats the database.
精彩评论