我理解异常意味着什么,我不明白的是为什么即使我提到谓词dataRow.RowState != DataRowState.Deleted
,标记为删除的行也会被删除
在我的'哪里'条款?
//DataSet is a strongly typed dataset
DataSet.TableB_row rowToDelete = dataSet.TableB.where(x=>x.ID==ID).First();
rowToDelete.Delete();
//don't want to use Remove(), might have to do RejectChanges depending on logic
//I do not want to call AcceptChanges just yet.
var results = (from rowA in dataSet.TableA
join rowB in dataSet.TableB
on rowA.ID equals rowB.ID
join rowC in dataSet.TableC
on rowB.ID equals rowC.ID
where rowB.RowState != DataRowState.Deleted
//added RowState check for other tables hoping that might fix, but didn't
&& rowA.RowState != DataRowState.Deleted
&& rowC.RowState != DataRowState.Deleted
&& rowA.ID == someID
select new { rowB.ColA, rowC.ColA });
foreach (var result in results)
{
//deletedRowInaccessibleException thrown
//some logic, not modifying the collection. Doesn't even get this far.
}
当然我做错了什么,但无法弄明白。关于SO的类似帖子,主要建议添加RowState检查,我已经拥有。
答案 0 :(得分:2)
在未接受更改的情况下,加入似乎会尝试评估已删除行的ID,然后使用 where 子句对其进行过滤。要解决此问题,您可以先创建“bRows”查询并在 join 中使用该查询,而不是直接使用TableB。这样的事情。
var bRows = dataset.TableB.Where(r => r.RowState != DataRowState.Delete);
var results = (from rowA in dataset.TableA
join rowB in bRows
on rowA.ID equals rowB.ID
join rowC in dataset.TableC
on rowB.ID equals rowC.ID
select new { rowB.ColA, rowC.ColA });