Reputation: 1673
Basically I have a DataTable A (see - https://i.sstatic.net/38RKc.png) which contains dates with their corresponding days.
I also have another DataTable B which contains the list of days a retailer can delivery on (see - https://i.sstatic.net/DTmD4.png)
What I want to do is loop through each row in DataTable A and if the day is in DataTable B then display it to the screen.
So far I have this but now I am stuck.
// Firstly call the stored procedure to obtain the list available delivery dates (this is basically today plus 14 days)
DataTable availableDatesRecord = new DataTable();
B2B.Data.CometB2BDB comet = new CometB2BDB();
StoredProcedure proc = comet.GetListOfAvailableDates(now);
DbDataReader reader = proc.ExecuteReader();
availableDatesRecord.Load(reader);
// Now we need to obtain the list of days we can deliver - this is all based on their postcode.
DataTable possibleDeliveryDayRecord = new DataTable();
proc = comet.GetDeliveryDatesByPostcode(postcode);
reader = proc.ExecuteReader();
possibleDeliveryDayRecord.Load(reader);
DataRow deliveryDays = possibleDeliveryDayRecord.Rows[1];
foreach (DataRow row in availableDatesRecord.Rows)
{
string deliveryDay = row["Day"].ToString();
}
What is the most efficient way of doing this?
Steven
Upvotes: 1
Views: 2733
Reputation: 2481
joining list and a datatable is may be what you want. Can you use Linq? if so this was answered here
if you are still in 2.0, then i would just do nested loop on datarows, something like
List<string> days;
foreach(DataRow dr in table.Rows)
if days.Contains(dr[columnname])
Console.WriteLine(dr[columnname]);
Upvotes: 1
Reputation: 476614
This simple method provides some basic functionality:
String[] days = {"monday","tuesday","sunday"};
DataTable table = new DataTable();
DataColumn dc = table.Columns.Add("day",typeof(string));
table.Rows.Add(days[0]);
table.Rows.Add(days[2]);
//query
foreach(string day in days) {
foreach(DataRow row in table.Rows) {
if(row[dc] == day) {
Console.WriteLine("Row {0} contains {1}",row,day);
}
}
Console.WriteLine();
}
Upvotes: 0