How can I filter a datatable without using LINQ? I am currenlty using .NET 2.0; therefore, I am unable to use LINQ. I have a stored procedure开发者_开发技巧 that returns room/rate pairs. I want to filter the datatable so it will select all the rates for a particular room, so essentially something like this:
SELECT Rates FROM TABLE1 WHERE Room = @Room.
Can I do this with a DataTable or is it better just to create another stored procedure to avoid using inline sql?
You can use the filter property of the default view, like this:
dt.DefaultView.RowFilter = "SomeColumn > 0";
dt = dt.DefaultView.ToTable();
You can also use the Select method too:
dt = dt.Select("SomeColumn > 0").CopyToDataTable();
you CAN do it with DataTable's Select method
// if table is your DataTable
DataRow[] foundRows =table.Select("Room = " + roomName);
as for deciding IF you want to do it this way or if it's better to rewrite your stored procedure to accept a @room parameter, it depends: if there aren't many total rows and if you're changing the selected room often then it might be better performance-wise to do it the way you're doing (keeping one total DataTable in memory and filtering it for different rooms)
surely a stored procedure would be the way to go, consider that you can also have the parameter set to NULL
in the stored so you ignore it when null or use it when provided with a value.
from the .NET side you can also select a subset of DataRows of a DataTable which satisfy a filter condition, something like this:
DataRow[] rows = myTable.Select("Room = '1'");
but as I said, server side filtering is better, less data moving in the network....
精彩评论