日期:2014-05-18 浏览次数:21219 次
DataTable dt = new DataTable(); dt.Columns.Add("批号", typeof(string)); dt.Columns.Add("数量", typeof(int)); DataRow dr1 = dt.NewRow(); dr1["批号"] = "0001"; dr1["数量"] = 100; dt.Rows.Add(dr1); DataRow dr2 = dt.NewRow(); dr2["批号"] = "0002"; dr2["数量"] = 200; dt.Rows.Add(dr2); DataRow dr3 = dt.NewRow(); dr3["批号"] = "0001"; dr3["数量"] = 300; dt.Rows.Add(dr3); DataRow dr4 = dt.NewRow(); dr4["批号"] = "0002"; dr4["数量"] = 400; dt.Rows.Add(dr4); DataRow dr5 = dt.NewRow(); dr5["批号"] = "0003"; dr5["数量"] = 500; dt.Rows.Add(dr5); var sumObject = dt.Compute("Sum(数量)", ""); var vardt = from p in dt.AsEnumerable() group p by p.ItemArray[0] into g let sum = g.Sum(x => int.Parse(x.ItemArray[1].ToString())) select new { 批号 = g.Key, Sum = sum }; foreach (var outputvar in vardt) { MessageBox.Show(outputvar.批号 + " " + outputvar.Sum); }
var qureysum = (from p in dt.AsEnumerable() select p.ItemArray[1]).Sum(x => int.Parse(x.ToString()));
var query = from x in db.Users let sexstring = x.sex ? "男" : "女" select sexstring;
------解决方案--------------------
写一个复杂的给你看看:(当然这么写只是为了演示,没有意义)
var query = from x in db.Users let theage = x.age let sexstring = x.sex ? "男" : "女" where theage > 30 && sexstring == "男"; select x;
------解决方案--------------------
来学习一下LINQ,好资源啊!