Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Merge DataTable Rows
Message
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01497405
Message ID:
01499303
Views:
35
>>>>>>Assume I have a datatable in a dataset with columns called ItemName and Quantity. Due to changes by the customer, there will now be multiple rows with the same ItemName and a Quantity of 1. The app used to have one row for the item and the total quantity.
>>>>>>
>>>>>>How do I combine the DT so that there is again one row with the total quantity?
>>>>>
>>>>>Do you want to save the totalled datatable to the backend? If so will this just be a one-off (tidy up) operation ?
>>>>>Or do you just need the totalled version to work with temporarily in the app ?
>>>>
>>>>Temporarily. It wont be sent to the DB.
>>>
>>>Something like:
var v = from x in dataTable.AsEnumerable()
>>>                    group x by x.Field<string>("ItemName") into g
>>>                    select new
>>>                    {
>>>                        ItemName = g.Key,
>>>                        Quantity = g.Sum(x => x.Field<int>("Quantity"))
>>>                    };
>>>
>>>//The above might be enough to work with?
>>>//If not then this would get it into a revised DataTable - but there's probably something better....
>>>
>>>            List<DataRow> al = new List<DataRow>();
>>>
>>>            foreach (var x in v)
>>>            {
>>>                DataRow dr = dataTable.NewRow();
>>>                dr["ItemName"] = x.ItemName;
>>>                dr["Quantity"] = x.Quantity;
>>>                al.Add(dr);
>>>            }
>>>            dataTable.Rows.Clear();
>>>            foreach (DataRow dr in al)
>>>            {
>>>                dataTable.Rows.Add(dr);
>>>            }
>>
>>Hey Viv,
>>
>>I implemented this code, and while is does merge the rows, the only columns with data in them are Part_Number (Sorry, it's not called ItemName ) and Quantity. I tried this:
>>
>>
>>
>>private void CombineDuplicateRows()
>>{
>>    DataTable table = csCOSApp.ActiveProject.dsDataStore.Tables[0];
>>
>>    var v = from x in table.AsEnumerable()
>>            group x by x.Field<string>("Part_Number") into g
>>            select new
>>            {
>>                Part_Number = g.Key,
>>                Quantity = g.Sum(x => x.Field<int>("Quantity")),
>>                RowId = x.RowId,
>>                ProjectId = x.ProjectId,
>>                ProductId = x.ProductId,
>>                Product_Series = x.Product_Series,
>>                Product_Number = x.Product_Number,
>>                Product_Name = x.Product_Name,
>>                Product_Description = x.Product_Description,
>>                PartId = x.PartId,
>>                Part_Description = x.Part_Description,
>>                New_Part_Number = x.New_Part_Number,
>>                Keyword = x.Keyword,
>>                Category = x.Category,
>>                TribalNumber = x.TribalNumber,
>>                WhseQty1 = x.WhseQty1,
>>                WhseQty2 = x.WhseQty2,
>>                PartPrice = x.PartPrice,
>>                PartRatio = x.PartRatio
>>            };
>>
>>    List<DataRow> al = new List<DataRow>();
>>
>>    foreach (var x in v)
>>    {
>>        DataRow dr = table.NewRow();
>>        dr["Part_Number"] = x.Part_Number;
>>        dr["Quantity"] = x.Quantity;
>>        al.Add(dr);
>>    }
>>
>>    table.Rows.Clear();
>>    foreach (DataRow dr in al)
>>    {
>>        table.Rows.Add(dr);
>>    }
>>}
>>
>>
>>but it doesn't know what 'x' is in the anonymous type. My Linq is not too good here. Could use some help.
>
>Hi,
>'v' is an instance of IGrouping < string,DataRow > so you could reach in to the first row of the group:
var v = from x in dataTable.AsEnumerable()
>                    group x by x.Field<string>("Part_Number") into g
>                    select new
>                    {
>                        Part_Number = g.Key,
>                        Quantity = g.Sum(x => x.Field<int>("Quantity")),
>                        ProjectId = ((DataRow)g.First())["ProjectId"],
>                        ProductId = ((DataRow)g.First())["ProductId"]
>                        //Etc
>                     };
I've a feeling there's a better way but I guess this works....

That did it. Thanks. I'm not too worried about design as I'm going to be refactoring this app soon anyhow.
Everything makes sense in someone's mind
public class SystemCrasher :ICrashable
In addition, an integer field is not for irrational people
Previous
Reply
Map
View

Click here to load this message in the networking platform