Deleted Data Row and LINQ

Another interesting programming problem. I have the following code in VB.Net which throws error:

dataRow.Delete() 'One of the rows in dataSet

dataSet.Tables("TableName").Rows.Cast(Of DataRow).FirstOrDefault(Function(dr) dr("ColumnName") = certainCondition)

The error says "Deleted row information cannot be accessed through the row". Pretty clear message, so I did the following:

dataSet.Tables("TableName").Rows.Cast(Of DataRow).FirstOrDefault(Function(dr) dr("ColumnName") = certainCondition AndAlso dr.RowState <> DataRowState.Deleted)

But the code above throws the same error. Apparently the deleted check has to be the first condition which again makes sense. Thus, the following code runs perfectly fine:

dataSet.Tables("TableName").Rows.Cast(Of DataRow).FirstOrDefault(Function(dr) dr.RowState <> DataRowState.Deleted AndAlso dr("ColumnName") = certainCondition)

There you go.

Comments

Popular posts from this blog

AWS EC2 Can't Reach EC2 Metadata Service After Subnet Change

A2 Hosting with .NET Core 2.1

Xcode CodeSign Incorrectly States Password is Incorrect