Reputation: 2070
I have this Linq statement that filters transactions. It works fine when it filters but I get an error in dt.AsEnumerable() when there is nothing being returned.
The error is Data contains no row. Anybody know how to handle when there is nothing returned?
newDataTable = dt.AsEnumerable()
.Where(r => !ListLinkedIds.Contains(r.Field<int>("LinkedTicketId")))
.CopyToDataTable();
gvMain.DataSource = newDataTable;
gvMain.DataBind();
Upvotes: 1
Views: 696
Reputation: 460098
You cannot use CopyToDataTable
if the input sequence is empty. So you need to check that first:
var newDataTable = dt.Clone(); // an empty table with the same schema
var ticketRows = dt.AsEnumerable()
.Where(r => !ListLinkedIds.Contains(r.Field<int>("LinkedTicketId")));
if(ticketRows.Any())
newDataTable = ticketRows.CopyToDataTable();
Possible exceptions with CopytoDataTable
Upvotes: 3
Reputation: 5689
Check if your DataTable
has any rows before calling AsEnumerable()
if (dt.Rows.Count > 0)
{
newDataTable = dt.AsEnumerable()
.Where(r => !ListLinkedIds.Contains(r.Field<int>("LinkedTicketId")))
.CopyToDataTable();
gvMain.DataSource = newDataTable;
gvMain.DataBind();
}
else {
//error
}
Upvotes: 0