这里我有两个DataTable
每个都有10k +值。我需要删除匹配的行 下面的代码工作正常
foreach (DataRow rw in dtTotalDateRangeExcludeSundays.Select())
{
DateTime dateFromRange=Convert.ToDateTime(rw[0].ToString());
string strPrnt = dateFromRange.ToShortDateString();
foreach (DataRow row in dtAbsentsLeavesHolidaysWorks.Select())
{
DateTime getDate = Convert.ToDateTime(row[0].ToString());
string strchild = getDate.ToShortDateString();
if (strPrnt == strchild)
{
rw.Delete();
}
}
}
想知道是否有更好的替代建议,当批量数据存在时,coz Foreach
循环需要时间
答案 0 :(得分:1)
您可以同时按日期和提前两种方式订购DataTables,同时保留日期作为参考。
像这样的东西(伪代码):
var CurrDate = Datetime.MinDate;
while true {
while (dtTotalDateRangeExcludeSundays[i].Date <= CurrDate) {
i++
}
while (dtAbsentsLeavesHolidaysWorks[j].Date <= CurrDate) {
j++
}
If (dtTotalDateRangeExcludeSundays[i].Date == dtAbsentsLeavesHolidaysWorks[j].Date) {
// delete
}
CurrDate = Min( dtAbsentsLeavesHolidaysWorks[j].Date, dtAbsentsLeavesHolidaysWorks[j].Date )
}
您必须添加索引超出范围和其他安全的所有检查。你明白了吗?
或强>
您可以在一个sql语句中删除所有匹配的行(如果适用)
Delete from TotalDateRangeExcludeSundays where Date in (Select date from AbsentsLeavesHolidaysWorks)