从DataTable过滤数据 [英] Filter data from DataTable

查看:133
本文介绍了从DataTable过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DataTable dtt = (DataTable)Session["ByBrand"];
var filldt = (dtt.Select("Price >= " + HiddenField1.Value + " and Price <= " + HiddenField2.Value + "")).CopyToDataTable();

此代码工作正常时,它发现价值在选定的数据表,但它显示的时候都没有发现价值的错误在数据表。所以,请告诉我如何检查,如果发现没有记录。

this code is working fine when it found values in selected DataTable but it showing error when Values are not found in DataTable. So please tell me how to check if no record found.

推荐答案

简单检查,如果你选择返回什么?

Simply check if your Select returns anything?

 DataTable dtt = (DataTable)Session["ByBrand"];
 DataRow[] rows = dtt.Select("Price >= " + HiddenField1.Value + " and Price <= " + HiddenField2.Value + "");
if(rows.Length > 0)
{
    var filldt = rows.CopyToDataTable();
}



好了,从蒂姆·LINQ的例子是非常好的,但要完成我的答案。
的选择方法总是返回一个DataRow数组也如果没有选定的行,但你不能要求从该空数组建立一个DataTable。想想吧。在CopyToDataTable应该建立什么模式的结果表,如果没有行是数组中存在的?

Well, the Linq example from Tim is really nice, but to complete my answer. The Select method returns Always a DataRow array also if there is no row selected, but then you cannot ask to build a datatable from this empty array. Think about it. What schema the CopyToDataTable should build for the resulting table if no rows are present in the array?

这篇关于从DataTable过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆