首页 > 代码库 > Datatable使用lambda表达式
Datatable使用lambda表达式
void DatatableLambdaTest()
{
DataTable dt = new DataTable();
dt.Columns.Add("userid");
dt.Columns.Add("username");
dt.Columns.Add("class");
DataRow dr = dt.NewRow();
dr["userid"] = "0";
dr["username"] = "张三";
dr["class"] = "一班";
dt.Rows.Add(dr);
DataRow dr1 = dt.NewRow();
dr1[0] = "1";
dr1[1] = "李四";
dr1[2] = "二班";
dt.Rows.Add(dr1);
DataRow dr2 = dt.NewRow();
dr2[0] = "3";
dr2[1] = "王五";
dr2[2] = "一班";
dt.Rows.Add(dr2);
dt.AcceptChanges();
var result =dt.AsEnumerable().Where<DataRow> (a => a["class"].ToString() == "一班");
int row=result.Count();
Console.WriteLine("一班有"+row+"个学生");
foreach(DataRow drn in result)
{
Console.WriteLine("userid:" + drn["userid"].ToString() + ",username:" + drn["username"].ToString() + ",class:" + drn["class"].ToString());
}
}
调用输出结果为:
Datatable使用lambda表达式