LinQ中的SortBy+sum+count的用法

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/p/2758421.html