daljit
daljit

Reputation: 1266

Filter Datatable with linq by considering deleted rows

I have a following datatable -

static DataTable GetTable()
{
  //
  // Here we create a DataTable with four columns.
  //
DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));

//
// Here we add five DataRows.
//
table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
return table;
}

// taken from following link http://www.dotnetperls.com/datatable

On PageLoad I do the following code -

DataTable dt = GetTable();
dt.AcceptChanges();
dt.Rows[0].Delete();
var t = dt.AsEnumerable().Where(dataRow => dataRow.Field<string>        ("Drug").Equals("Enebrel"));

It throws an error, that unable to get the data from deleted rows.

Is there any way to filter tell linq that do not take into account deleted rows.

Thanks, Daljit Singh

Upvotes: 1

Views: 4612

Answers (1)

Mark Harrell
Mark Harrell

Reputation: 331

You could specify to exclude the deleted row in your linq with something like this

var t = dt.AsEnumerable().Where(dataRow => dataRow.RowState != DataRowState.Deleted &&...

That should do the trick

Upvotes: 7

Related Questions