using System; using System.Data; namespace DataStreams.ETL { /// /// Allows quick lookups on DataTables based on column values instead of using the super /// slow Select method of a DataTable. /// public class DataTableIndex { private DataView lookupTable = null; public DataTableIndex(DataTable data, params DataColumn[] index) { bool first = true; string sort = ""; // create a comma separated list of sort columns foreach (DataColumn column in index) { if (!first) { sort += ","; } first = false; // use brackets to handle column names with spaces, etc sort += "[" + column.ColumnName + "]"; } // use a DataView because it internally creates an index to cover the sort criteria lookupTable = new DataView( data, null, sort, DataViewRowState.CurrentRows); } /// /// Searches for DataRow's using an indexed lookup. /// /// /// Value order must directly match the order of the columns passed in to the constructor. /// /// /// The matching DataRow's. /// public DataRow[] Find(params object[] value) { DataRowView[] found = lookupTable.FindRows(value); DataRow[] matchingRows = new DataRow[found.Length]; for (int i = 0; i < found.Length; i++) { matchingRows[i] = found[i].Row; } return matchingRows; } } }