Lamdba及DataTable AsEnumerable()的使用()

1.Where语句使用

var var_dtTable = dtTable.AsEnumerable().Where<DataRow>(W => W["NAME"].ToString()=="张三");
 
int count = var_dtTable.Count<DataRow>();//获取dtTable中NAME为张三的个数

2.Average语句使用

 int avg = dtTable.AsEnumerable().Average<DataRow>(A => float.Parse(A[“avg_name”].ToString())) //获取dtTable中avg_name列的平局数 

3.GroupBy

DataTable dtTable = new DataTable();//数据源
DataTable dtResult = new DataTable();
dtResult.Columns.Add("groupBy_name", typeof(string));
dtResult.Columns.Add("sum_name", typeof(int));
dtResult.Columns.Add("count_name", typeof(int));
var var_dtTable= dtTable.AsEnumerable().GroupBy(G => G["groupBy_name"].ToString()).Select(S => new {
               groupBy_name= s.Key,//获取分组的值
               sum_name= s.Sum(m => m.Field<int>("sum_name")),//获取列名为sum_name数据的和
               count_name= s.Count(m => m.Field<int>("count_name"))//获取列名为count_name数据的个数
               });
var_dtTable.ToList().ForEach(f=> dtResult.Rows.Add(f.groupBy_name,f.sum_name,f.count_name);
//对应的顺序为dtResult列的顺序

4.OrderBy

DataTable dtTable = new DataTable();//数据源
dtTable = dtTable.AsEnumerable().OrderByDescending(o => o["OrderBy_name"]).CopyToDataTable();//降序
dtTable = dtTable.AsEnumerable().OrderBy(o => o["OrderBy_name"]).CopyToDataTable();//升序

5.Distinct

DataTable dtTable = new DataTable();//数据源
dtTable = dtTable.AsEnumerable().Distinct(new ColumnEquals("column_name")).CopyToDataTable();
//column_name是要Distinct列的名字
————————

1.Where语句使用

var var_dtTable = dtTable.AsEnumerable().Where<DataRow>(W => W["NAME"].ToString()=="张三");
 
int count = var_dtTable.Count<DataRow>();//获取dtTable中NAME为张三的个数

2.Average语句使用

 int avg = dtTable.AsEnumerable().Average<DataRow>(A => float.Parse(A[“avg_name”].ToString())) //获取dtTable中avg_name列的平局数 

3.GroupBy

DataTable dtTable = new DataTable();//数据源
DataTable dtResult = new DataTable();
dtResult.Columns.Add("groupBy_name", typeof(string));
dtResult.Columns.Add("sum_name", typeof(int));
dtResult.Columns.Add("count_name", typeof(int));
var var_dtTable= dtTable.AsEnumerable().GroupBy(G => G["groupBy_name"].ToString()).Select(S => new {
               groupBy_name= s.Key,//获取分组的值
               sum_name= s.Sum(m => m.Field<int>("sum_name")),//获取列名为sum_name数据的和
               count_name= s.Count(m => m.Field<int>("count_name"))//获取列名为count_name数据的个数
               });
var_dtTable.ToList().ForEach(f=> dtResult.Rows.Add(f.groupBy_name,f.sum_name,f.count_name);
//对应的顺序为dtResult列的顺序

4.OrderBy

DataTable dtTable = new DataTable();//数据源
dtTable = dtTable.AsEnumerable().OrderByDescending(o => o["OrderBy_name"]).CopyToDataTable();//降序
dtTable = dtTable.AsEnumerable().OrderBy(o => o["OrderBy_name"]).CopyToDataTable();//升序

5.Distinct

DataTable dtTable = new DataTable();//数据源
dtTable = dtTable.AsEnumerable().Distinct(new ColumnEquals("column_name")).CopyToDataTable();
//column_name是要Distinct列的名字