Reputation: 885
I am filtering data from datatable
using linq
, it is working fine, except when search criteria doesn't match, the resultant table is not handled for emptiness.
Below is my code:
var table = dtTokensInfo.AsEnumerable()
.Where(r => r.Field<string>("tokenName").Contains(txtTokenName.Text))
.CopyToDataTable();
I want table to contain values as filtered from dtTokensInfor, but am not able to handle it when search criteria doesn't match
Upvotes: 0
Views: 89
Reputation: 216348
You should detach the call to CopyToDataTable from the result of the Linq expression
DataTable table = null;
var temp = dtTokensInfo.AsEnumerable()
.Where(r => r.Field<string>("tokenName")
.Contains(txtTokenName.Text));
if(temp != null)
table = temp.CopyToDataTable();
If you look at the MSDN page about CopyToDataTable IEnumerable extension you will notice this between the possible exceptions thrown
ArgumentNullException: The source IEnumerable(Of T) sequence is Nothing and a new table cannot be created.
Upvotes: 1