通过Linq 实现DataTable Group By
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("A");
dt.Columns.Add("B");
dt.Columns.Add("C");
dt.Rows.Add("a1", "b1", "c1");
dt.Rows.Add("a1", "b2", "c2");
dt.Rows.Add("a2", "b3", "c3");
dt.Rows.Add("a3", "b4", "c4");
dt.Rows.Add("a1", "b5", "c5");
dt.Rows.Add("a2", "b6", "c6");
IEnumerable<IGrouping<string, DataRow>> result = dt.Rows.Cast<DataRow>().GroupBy<DataRow, string>(dr => dr["A"].ToString());//按A分组
foreach (IGrouping<string, DataRow> ig in result)
{
Console.WriteLine("当A="+ig.Key + ":");
Console.WriteLine("A".PadRight(10) + "B".PadRight(10) + "C".PadRight(10));
Console.WriteLine("=====".PadRight(10) + "=====".PadRight(10) + "=====".PadRight(10));
foreach (var dr in ig)
Console.WriteLine(dr["A"].ToString().PadRight(10) + dr["B"].ToString().PadRight(10) + dr["C"].ToString().PadRight(10));
}
Console.Read();
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/wwlearn/archive/2010/05/06/5562527.aspx