I am using ASP.NET 3.5 with VB.net
I have a DataTable as such.
RpID LocationServed
---- --------------
1 St. Louis
1 Baltimore
1 Columbus
2 Chicago
2 St. Charles
2 Peoria
2 Nashville
3 Dallas
3 Miami
3 Indianapolis
What that for each Rep, I need to send an email with all of their Location Served.
I am not quite sure how do do this. I am using ASP.NET 4
My plan is to have 2 loops
<loop1 that has the group by of RepID>
<loop2 can go through the list of items for a given RepID >
strCity = do processing here that will compile a list of the cities for a given rep
</loop2 end>
<send out email a given RepID and then reset strCity >
<loop1 end>
But not quite sure what the best approach is to tackle this.
You can do this one of three ways.
Assuming the DataTable is sorted by RpID, have one loop, process each city. When RpID changes, send an email.
OR…
Loop on the data table. When RpID changes, call .Select on the data table and filter on RpID. Loop on the returned array of DataRows and process each city. Send an email.
OR…
Since you are using ASP.NET 4, you can use a LINQ query to get a distinct list of RpID from the datatable, loop on that list of RpIDs, and then in the loop, use a LINQ query on the datatable to get a list of cities from the datatable for that RpID. Send an email. See here (this talks about a DataSet, but it’s the same thing, really)
Note that for any of these approaches, you will have to have a check after the loop to send an email for the last RpID processed.