Source contains no datarow (LINQ)

Hi all,

Facing an issue when trying to filter a datatable using LINQ.

DTFiltered = DTUnfiltered.AsEnumerable.Where(Function(r) CStr(r(“ColumnA”).ToString).Contains(“TEST”)).CopyToDatatable

With the above statement, I get the error “Source contains no datarow”. This is true since DTUnfiltered does not always contain TEST. To solve this, I created the following IF statement:
DTFiltered=if(DTUnfiltered.AsEnumerable.Where(Function(r) CStr(r(“ColumnA”).ToString).Contains(“TEST”)).Any(),DTUnfiltered.AsEnumerable.Where(Function(r) CStr(r(“ColumnA”).ToString).Contains(“TEST”)).CopyToDataTable(), DTHitsAchternaam.Clone)

While this statements works perfectly, it seems a bit unnecessary to loop the DT twice. Does anyone have an alternative for the second loop through the DT?

Kind regards,
Mike

@Miketb Check this post :

Thank you!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.