C#-检查字符串是否在DataRow中
本文关键字:DataRow 是否 检查 字符串 C#- | 更新日期: 2023-09-27 18:27:48
基本上我有一个DataTable a(请参阅-https://i.stack.imgur.com/38RKc.png)其包含具有相应日期的日期。
我还有另一个数据表B,其中包含零售商可以交货的天数列表(请参阅-https://i.stack.imgur.com/DTmD4.png)
我想做的是循环浏览数据表A中的每一行,如果日期在数据表B中,则将其显示在屏幕上。
到目前为止,我有这个,但现在我被卡住了。
// 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();
}
做这件事最有效的方法是什么?
Steven
连接列表和数据表可能是您想要的。你会用林克吗?如果是的话,这里回答
如果你还在2.0中,那么我只会在数据行上进行嵌套循环,比如
List<string> days;
foreach(DataRow dr in table.Rows)
if days.Contains(dr[columnname])
Console.WriteLine(dr[columnname]);
这个简单的方法提供了一些基本功能:
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();
}