I am writing this in C# using .NET 3.5. I have a System.Data.DataSet object with a single DataTable that uses the following schema:
Id : uint AddressA: string AddressB: string Bytes : uint
When I run my application, let’s say the DataTable gets filled with the following:
1 192.168.0.1 192.168.0.10 300 2 192.168.0.1 192.168.0.20 400 3 192.168.0.1 192.168.0.30 300 4 10.152.0.13 167.10.2.187 80
I’d like to be able to query this DataTable where AddressA is unique and the Bytes column is summed together (I’m not sure I’m saying that correctly). In essence, I’d like to get the following result:
1 192.168.0.1 1000 2 10.152.0.13 80
I ultimately want this result in a DataTable that can be bound to a DataGrid, and I need to update/regenerate this result every 5 seconds or so.
How do I do this? DataTable.Select() method? If so, what does the query look like? Is there an alternate/better way to achieve my goal?
EDIT: I do not have a database. I’m simply using an in-memory DataSet to store the data, so a pure SQL solution won’t work here. I’m trying to figure out how to do it within the DataSet itself.
For readability (and because I love it) I would try to use LINQ:
If a performace issue is discovered with the LINQ solution I would go with a manual solution summing up the rows in a loop over the original table and inserting them into the result table.
You can also bind the aggregatedAddresses directly to the grid instead of putting it into a DataTable.