Let's go

Compute和Linq的Field使用

目录:

1.Compute

案例:

 1 private void ComputeBySalesSalesID(DataSet dataSet)
 2 {
 3     // Presumes a DataTable named "Orders" that has a column named "Total."
 4     DataTable table;
 5     table = dataSet.Tables["Orders"];
 6 
 7     // Declare an object variable.
 8     object sumObject;
 9     sumObject = table.Compute("Sum(Total)", "EmpID = 5");
10 }
1  DataTable dt = null;
2 dt=...  
3 decimal money = Convert.ToDecimal(dt.Compute("Sum(money)", "true"));

 

2.Field的使用(根据dt获取每个列的数据)

案例:

 1 // Fill the DataSet.
 2 DataSet ds = new DataSet();
 3 ds.Locale = CultureInfo.InvariantCulture;
 4 FillDataSet(ds);
 5 
 6 DataTable products = ds.Tables["Product"];
 7 
 8 var query =
 9     from product in products.AsEnumerable()
10     where product.Field<string>("Color") == "Red"
11     select new
12     {
13         Name = product.Field<string>("Name"),
14         ProductNumber = product.Field<string>("ProductNumber"),
15         ListPrice = product.Field<Decimal>("ListPrice")
16     };
17 
18 foreach (var product in query)
19 {
20     Console.WriteLine("Name: {0}", product.Name);
21     Console.WriteLine("Product number: {0}", product.ProductNumber);
22     Console.WriteLine("List price: ${0}", product.ListPrice);
23     Console.WriteLine("");
24 }

结合linq语句

 decimal money = dt.AsEnumerable().Sum(t => Math.Round((t.Field<Decimal>("money")) / 10000, 2));

 

posted @ 2018-07-18 10:23  chenze  阅读(319)  评论(0编辑  收藏  举报
有事您Q我