需要在动态表中合并2行

时间:2017-09-21 13:14:01

标签: c# asp.net c#-4.0

您好我已经创建了动态表,如下所示,我有2行具有相同的ID 我应该如何合并它们。

DataTable dt = new DataTable();
dt = (DataTable)Session["AddtoCart"];
DataRow dr1 = dt.NewRow();
foreach (var key in collection.AllKeys)
{
    dr1["Description"] = collection["hdDescription"];
    dr1["Title"] = collection["hdTitle"];
    dr1["ActualQuantity"] = collection["hdactualquantity"];
    dr1["PropertyId"] = collection["hdPropertyId"];
    dr1["Quantity"] = collection["Quantity"];
    TempData["AddedtoCart"] = ConfigurationManager.AppSettings["AddtoCart"].ToString();
}
dt.Rows.Add(dr1);

我添加的行为

Propertyid, Quantity, ActualQuantity
1             5          10
1             2          10
2             3          20
2             4          20

我需要的结果是

Propertyid, Quantity, ActualQuantity
1             7          10
2             7          20

更新:我尝试了this answer

var query = dt.AsEnumerable()
    .GroupBy(row => row.Field<int>("PropertyId"))// issue over this line
    .Select(grp => new
    {
        PropertyId = grp.Key,
        Quantity = grp.Sum(row => row.Field<int>("Quantity")),
        ActualQuantity = grp.First().Field<int>("ActualQuantity"),
        Title = grp.First().Field<int>("Title"),
        Description = grp.First().Field<int>("Description")
    });

var SumByIdTable = dt.Clone();
foreach (var x in query)
    SumByIdTable.Rows.Add(x.PropertyId, x.Quantity, x.ActualQuantity,x.Title, x.Description);

Session["AddtoCart"] = SumByIdTable; 

但由于Specified cast无效,我遇到了问题。 on .GroupBy(row =&gt; row.Field(&#34; PropertyId&#34;))

更新2 :我尝试了以下代码,但我遇到了问题

DataTable dt = new DataTable();
dt.Columns.Add("PropertyId", typeof(int));
dt.Columns.Add("Quantity", typeof(int));
dt.Columns.Add("Description", Type.GetType("System.String"));
dt.Columns.Add("Title", Type.GetType("System.String"));
dt.Columns.Add("ActualQuantity", typeof(int));
DataRow dr1 = dt.NewRow();
foreach (var key in collection.AllKeys)
{
    dr1["Description"] = collection["hdDescription"];
    dr1["Title"] = collection["hdTitle"];
    dr1["ActualQuantity"] = Convert.ToInt32(collection["hdactualquantity"]);
    dr1["PropertyId"] = Convert.ToInt32(collection["hdPropertyId"]);
    dr1["Quantity"] = Convert.ToInt32(collection["Quantity"]);
    TempData["AddedtoCart"] = ConfigurationManager.AppSettings["AddtoCart"].ToString();
}
dt.Rows.Add(dr1);

var query = dt.AsEnumerable()
    .GroupBy(row => row.Field<int>("PropertyId"))
    .Select(grp => new
    {
        PropertyId = grp.Key,
        Quantity = grp.Sum(row => row.Field<int>("Quantity")),
        ActualQuantity = grp.First().Field<int>("ActualQuantity"),
        Title = grp.First().Field<string>("Title"),
        Description = grp.First().Field<string>("Description")
    });

var SumByIdTable = dt.Clone();
foreach (var x in query)
    SumByIdTable.Rows.Add(x.PropertyId, x.Quantity, x.ActualQuantity,x.Title, x.Description);

Session["AddtoCart"] = SumByIdTable; 

但我在

中遇到问题

SumByIdTable.Rows.Add(x.PropertyId, x.Quantity, x.ActualQuantity,x.Title, x.Description);

  

输入字符串的格式不正确。

已解决的更新3 :我尝试过以下代码并且正在使用

DataTable dt = new DataTable();
dt.Columns.Add("PropertyId", typeof(int));
dt.Columns.Add("Quantity", typeof(int));
dt.Columns.Add("Description", Type.GetType("System.String"));
dt.Columns.Add("Title", Type.GetType("System.String"));
dt.Columns.Add("ActualQuantity", typeof(int));
DataRow dr1 = dt.NewRow();
foreach (var key in collection.AllKeys)
{
    dr1["Description"] = collection["hdDescription"];
    dr1["Title"] = collection["hdTitle"];
    dr1["ActualQuantity"] = Convert.ToInt32(collection["hdactualquantity"]);
    dr1["PropertyId"] = Convert.ToInt32(collection["hdPropertyId"]);
    dr1["Quantity"] = Convert.ToInt32(collection["Quantity"]);
    TempData["AddedtoCart"] = ConfigurationManager.AppSettings["AddtoCart"].ToString();
}
dt.Rows.Add(dr1);

var query = dt.AsEnumerable()
    .GroupBy(row => row.Field<int>("PropertyId"))
    .Select(grp => new
    {
        PropertyId = grp.Key,
        Quantity = grp.Sum(row => row.Field<int>("Quantity")),
        ActualQuantity = grp.First().Field<int>("ActualQuantity"),
        Title = grp.First().Field<string>("Title"),
        Description = grp.First().Field<string>("Description")
    });

var SumByIdTable = dt.Clone();
foreach (var x in query)
    SumByIdTable.Rows.Add(Convert.ToInt32(x.PropertyId), Convert.ToInt32(x.Quantity), x.Description, x.Title, Convert.ToInt32(x.ActualQuantity));

Session["AddtoCart"] = SumByIdTable; 

更改是我必须将SumByIdTable中的值添加为dt

中的克隆集

1 个答案:

答案 0 :(得分:2)

您可以使用LINQ(-to-DataTable):

var query = dt.AsEnumerable()
    .GroupBy(row => row.Field<int>("Propertyid"))
    .Select(grp => new { 
        Propertyid     = grp.Key,
        Quantity       = grp.Sum(row => row.Field<int>("Quantity")),
        ActualQuantity = grp.First().Field<int>("ActualQuantity")       
    });

var SumByIdTable = dt.Clone();
foreach(var x in query)
    SumByIdTable.Rows.Add(x.Propertyid, x.Quantity, x.ActualQuantity);