DataTable Relational Operators in C# - RESTRICT Method (WHERE)
In the Introduction, I pointed out that the DataTable has a method which is similar to the WHERE clause in TSQL.
This method (Select) returns an array of DataRow's that meet the criteria of your expression.
To bring this method in line with our other operators in returning a DataTable, a simple helper method is used.
public static DataTable RowsToTable(DataRow[] Rows)
{
DataTable table = new DataTable("Converted");
foreach(DataColumn column in Rows[0].Table.Columns)
{
table.Columns.Add(new DataColumn(column.ColumnName,column.DataType));
}
table.BeginLoadData();
foreach(DataRow row in Rows)
{
table.LoadDataRow(row.ItemArray,true);
}
table.EndLoadData();
return table;
}
public static DataTable Restrict(DataTable Table, string WhereClause)
{
return SQLOps.RowsToTable(Table.Select(WhereClause));
}
If you have stuck with this series of posts, I have one more post to go, then I am done.
It will be looking at the practical implementations of these methods against the Northwind database.