不使用 LINQ 过滤数据表?
如何在不使用 LINQ 的情况下过滤数据表?我目前正在使用 .NET 2.0;因此,我无法使用 LINQ。我有一个返回房间/价格对的存储过程。我想过滤数据表,以便它选择特定房间的所有费率,所以本质上是这样的:
SELECT Rates FROM TABLE1 WHERE Room = @Room.
我可以使用数据表执行此操作,还是创建另一个存储过程以避免使用内联 sql 更好?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用默认视图的过滤器属性,如下所示:
您也可以使用 Select 方法:
You can use the filter property of the default view, like this:
You can also use the Select method too:
您可以使用 DataTable 的 Select 方法
来决定是否要这样做这样做或者是否最好重写存储过程以接受 @room 参数,这取决于:
如果总行数不多并且您经常更改选定的房间,那么按照您的方式进行操作可能会更好(在内存中保留一个总数据表并针对不同的房间过滤它)
you CAN do it with DataTable's Select method
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)
当然,存储过程是可行的方法,考虑一下您也可以在存储中将参数设置为 NULL ,这样您就可以在 null 时忽略它,或者在提供值时使用它。
从.NET端,您还可以选择满足过滤条件的DataTable的DataRows子集,如下所示:
但正如我所说,服务器端过滤更好,网络中移动的数据更少......
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:
but as I said, server side filtering is better, less data moving in the network....