如何过滤数据表 [英] How to filter DataTable
本文介绍了如何过滤数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,先生/女士
我想过滤一个数据表.
为此,我以这种方式使用LINQ,它无法正常工作,并给出错误
Hello sir/madam
I would like to filter a datatable.
For that I am using LINQ in this way its not working its give an error
"The source contains no DataRows."
首先,我在数据表dt中得到一个值.
现在我正在使用LINQ
First i get a value in a datatable dt.
now i am using LINQ
var query = from r in dt.AsEnumerable()
where r.Field<string>("AI_No_Of_Conception") == ""
select r;
DataTable dtLink=new DataTable();
dtLink = query.CopyToDataTable();
如果还有其他选择如何在数据表上应用查询并将其绑定到另一个数据表,则建议使用my.
If any another option how to apply a query on datatable and bind it another Datatable then please suggest my.
推荐答案
嗨
试试吧,
Hi
Try this out,
var query = from r in dt.AsEnumerable() where
String.Equals(r.Field<string>("AI_No_Of_Conception"),"")
select r;
// any one please suggest an idea to optimize the following two lines
if(result.Count() >0)// to avoid exception "The source contains no DataRows"
dtLink = result.CopyToDataTable();
--SJ
--SJ
我前段时间也遇到了同样的问题,并发现根本原因很愚蠢,您需要像这样使用linq查询
I faced the same issue some time back and figured out that the rootcause is very silly you need to use linq query like this
var query = from r in dt.AsEnumerable()
where r.Field("AI_No_Of_Conception").ToString() == ""
select r;
只需使用Tostring()
just use Tostring()
这篇关于如何过滤数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文