筛选dataTable数据的2种方法
1:
DataView dv = new DataView() { Table = dt, RowFilter = wherecode,Sort = orderby}; //不要再排序 例如:wherecode = “did not in(‘1’,‘2’)”
return dv.ToTable();
2:Linq
linq本质上只能查到IEnumable类型。所以,DataTable有一个扩展方法:AsEnumerable。
var vdt = from temp in dt.AsEnumerable() // linq语句
where temp["CompanyName"].ToString().StartsWith(args)
select new
{ // 集合列表Eval()方法绑定的显示字段;
Comid = temp["Comid"].ToString(),
CompanyName = temp["CompanyName"].ToString(),
NCID = temp["NCID"].ToString(),
CreateDate = temp["CreateDate"].ToString(),
Phone = temp["Phone"].ToString()
};
this.Repeater1.DataSource = vdt;
this.Repeater1.DataBind();
又例如:
var a = from row in dt.AsEnumerable()
where row.Field<int>("age") <= 24
select row.Field<string>("name");
foreach (var s in a)