C# - linq 操作 DataTable

 1 方法一:更简洁
 2             Console.WriteLine(dt.Rows.OfType<DataRow>().First(x => x.Field<string>("xx_Name") == "A").Field<string>("xx_NO"));   
 3   
 4 方法二:linq 更清晰
 5   
 6             var query = from dr in dt.AsEnumerable()  
 7                               where dr.Field<string>("xx_Name") == "A"  
 8                               select dr;  
 9             Console.WriteLine(query.First().Field<string>("xx_NO"));
10  
11 通过linq对现有的DataTable进行查询,并将结果拷贝至新的DataTable中
12 // Query the SalesOrderHeader table for orders placed 
13 // after August 8, 2001.
14 IEnumerable<DataRow> query =
15     from order in orders.AsEnumerable()
16     where order.Field<DateTime>("OrderDate") > new DateTime(2001, 8, 1)
17     select order;
18  
19 // Create a table from the query.
20 DataTable boundTable = query.CopyToDataTable<DataRow>();

 

posted on 2019-11-19 11:23  空港  阅读(521)  评论(0编辑  收藏  举报