C# - 检查字符串是否在DataRow中

时间:2012-03-15 10:11:21

标签: c# datatable datarow

基本上我有一个DataTable A(参见 - http://i.imgur.com/cvTMB.png),其中包含相应日期的日期。

我还有另一个DataTable B,其中包含零售商可以交付的日期列表(请参阅 - http://i.imgur.com/fmSeg.png

我想要做的是遍历DataTable A中的每一行,如果当天在DataTable 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();
}

最有效的方法是什么?

史蒂芬

2 个答案:

答案 0 :(得分:1)

加入列表和数据表可能是你想要的。你能用Linq吗?如果是这样,那就回答了here

如果你仍然在2.0,那么我会在数据行上做嵌套循环,比如

List<string> days;
    foreach(DataRow dr in table.Rows)
      if days.Contains(dr[columnname])
        Console.WriteLine(dr[columnname]);

答案 1 :(得分:0)

这个简单的方法提供了一些基本功能:

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();
}
相关问题