var query = from p in dt.AsEnumerable()
//分组
group p by new
{
t1 = p.Field<int>("AA").ToString().Substring(0, p.Field<int>("AA").ToString().Length - 2),
t2 = p.Field<double>("BB"),
t3 = p.Field<string>("CC")
} into g
select new
{
ColA = g.Key.t1,
ColB = g.Key.t2,
ColC = g.Key.t3,
ColD = g.Sum(c =>c.Field<int>("DD"))
}
//转datatable
DataTable dtNew = dt.Clone()
DataRow drNew
foreach (var p in query)
{
drNew = dtNew.NewRow()
drNew["AA"] = p.ColA
drNew["BB"] = p.ColB
drNew["CC"] = p.ColC
drNew["DD"] = p.ColD
dtNew.Rows.Add(drNew)
}
dtNew 为最终结果
注:转换过程中要考虑空值的情况
c#连接MySql数据库的方法一、用MySQLDriverCS连接MySQL数据库。
先下载和安装MySQLDriverCS,在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中。
using System
using System.Collections.Generic
using System.ComponentModel
using System.Data
using System.Drawing
using System.Linq
using System.Text
using System.Windows.Forms
using MySQLDriverCS
namespace jxkh
{
public partial class frmLogin : Form
{
public frmLogin()
{
InitializeComponent()
}
private void btnLogin_Click(object sender, EventArgs e)
{
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)