合并数据行 c#

本文关键字:数据 合并 | 更新日期: 2023-09-27 18:34:51

>我正在使用由 xml 文件导入的数据表

因此,我将多个数据表合并为一个数据表,

并获取具有相同代理名称的行,并将其每列的值添加到一个数据行中,然后将其放入新表并将其显示在数据网格视图上例

输入:

Agent   Product1   Product2
  A        1          3
  B        4          5
  A        5          4

输出:

Agent   Product1   Product2
  A        6          7
  B        4          5

合并数据行 c#

希望这就是你要找的:) 只需将您的数据表传递给dt,您就可以在dtFinal中找到所需的输出

        DataTable dt = new DataTable();
        dt.Columns.Add("Agent");
        dt.Columns.Add("Product1");
        dt.Columns.Add("Product2");
        DataRow dr = dt.NewRow();
        dr[0] = "AA";
        dr[1] = 7;
        dr[2] = 5;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = "AA";
        dr[1] = 5;
        dr[2] = 6;
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr[0] = "BB";
        dr[1] = 2;
        dr[2] = 3;
        dt.Rows.Add(dr);


 DataView view = new DataView(dt);
        DataTable dtDaysInDocument = view.ToTable(true, "Agent");**//Used to distinct column value**
        string[] dateRecord = dtDaysInDocument.Rows.Cast<DataRow>()
                            .Select(row => row["Agent"].ToString())
                            .ToArray();

        DataTable dtFinal = new DataTable();
        dtFinal = dt.Clone();
        foreach (string str in dateRecord)
        {
            DataRow[] rows = dt.Select("Agent='" + str + "'");
            if (rows.Length > 1)
            {
                int finalValue = 0;
                foreach (DataColumn col in dt.Columns)
                {
                    if (col.ColumnName != "Agent")
                    {
                        foreach (DataRow row in rows)
                        {
                            int colValue = row[col] != null ? Convert.ToInt16(row[col]) : 0;
                            finalValue = colValue;
                            DataRow[] dtFinalrows = dtFinal.Select("Agent='" + str + "'");
                            if (dtFinalrows.Length == 1)
                            {
                                string val = dtFinalrows[0][col.ColumnName].ToString();
                                if (val != "")
                                    finalValue = finalValue + Convert.ToInt16(val);
                                dtFinalrows[0][col.ColumnName] = finalValue;
                            }
                            else
                            {
                                DataRow finalDr = dtFinal.NewRow();
                                finalDr[col.ColumnName] = finalValue;
                                finalDr["Agent"] = str;
                                dtFinal.Rows.Add(finalDr);
                            }
                        }
                    }
                }

            }
            else if (rows.Length == 1)
            {
                DataRow finalDr = dtFinal.NewRow();
                foreach (DataColumn col in dt.Columns)
                {
                    finalDr[col.ColumnName] = rows[0][col.ColumnName];
                }
                dtFinal.Rows.Add(finalDr);
            }

使用 Linq:

from t in dt.AsEnumerable()
group t by t["Agent"]
into newtable
select new {
 Agent = newtable.Key;
 Product1 = newtable.Sum(c=>c["Product1"]);
 Product2 = newtable.Sum(c=>c["Product2"]);
}

试试这个

            DataTable dt1 = new DataTable();
            dt1.Columns.Add("Agent", typeof(string));
            dt1.Columns.Add("Product1", typeof(int));
            dt1.Columns.Add("Product2", typeof(int));
            dt1.Rows.Add(new object[] {"A", 1, 3});
            dt1.Rows.Add(new object[] {"B", 4, 5});
            dt1.Rows.Add(new object[] {"A", 5, 4});
            DataTable dt2 =  dt1.Clone();
            var sum = dt1.AsEnumerable()
                .GroupBy(x => x.Field<string>("Agent"))
                .Select(m => new
                {
                    key = m.Key,
                    p1 = m.Sum(s => s.Field<int>("Product1")),
                    p2 = m.Sum(s => s.Field<int>("Product2"))
                }).ToList();
            foreach (var row in sum)
            {
                dt2.Rows.Add(new object[] { row.key, row.p1, row.p2 });
            }
            dataGridView1.DataSource = dt2;