static DataTable GetData() { DataTable table = new DataTable(); table.Columns.Add("Name", typeof(string)); table.Columns.Add("Quantity", typeof(int)); table.Rows.Add("a", 1); table.Rows.Add("a", 2); table.Rows.Add("b", 2); table.Rows.Add("b", 2); table.Rows.Add("c", 1); table.Rows.Add("c", 2); table.Rows.Add("c", 3); table.Rows.Add("c", 4); return table; }
static void Main() { Console.WriteLine("1111"); DataTable otable = GetData();
var query = from d in otable.AsEnumerable() group d by d.Field<string>("Name") into g select new { c2 = g.Key, c3=g.Count(), c4 = g.Sum(t => t.Field<int>("Quantity")) }; query.ToList().ForEach(q => Console.WriteLine("{0},{1},{2}", q.c2, q.c3,q.c4));
Console.ReadLine(); }
转载于:https://www.cnblogs.com/wonder223/archive/2012/11/07/2758421.html
相关资源:Linq基础学习资料,通俗易懂