标签:ring first rabl detail field wro for exec mysq
因为程序的特殊情景,需要在sql查出来的DataTable进行分组,DataTable分组可以使用linq,也可以自己写分组程序。
linq相对简单:
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.CommandTimeout = 300;
MySqlDataReader reader = cmd.ExecuteReader();
DataTable tb = new DataTable();
detailTable.Load(reader);
reader.Close();
var query = from c in tb .AsEnumerable()
group c by new {
pingming = c.Field<string>("品名"),
guige = c.Field<string>("规格")
}
into s
select new
{
pingming = s.Select(p => p.Field<string>("品名")).First(),
shuliang = s.Sum(p => Convert.ToInt32(p.Field<string>("数量"))),
guige = s.Select(p => p.Field<string>("规格")).First(),
biaohao = string.Join(";",s.Select(p => p.Field<string>("表号")))
};
DataTable tbc = tb.Clone();
query.ToList().ForEach(p =>tbc.Rows.Add(p.pingming, p.guige,p.biaohao,p.shuliang));//同下面foreach,只不过下面版本循环体可以做一些逻辑操作
foreach(var row in query )
{
DataRow r = tbc.NewRow();
r["pingming"]=row.pingming;
r["guige "]=row.guige ;
r["biaohao "]=row.biaohao;
r["shuliang "]=row.shuliang ;
tbc.Rows.Add(r);
}
对DataTable进行分组
标签:ring first rabl detail field wro for exec mysq
原文地址:http://www.cnblogs.com/meihuizi/p/6411088.html