Issue with linq query of getting unmatched data

I’m using this query and it is working fine but when there is no data it throws error

I’m using assign activity

Dt3 = dt1.AsEnumerable().Where(Function (row) Not Dt2.AsEnumerable().Select(Function(r) r.Field(Of String)(“column name”)).Any(Function (x) x=row.Field(of string)(“column name”))).CopyToDatatable()

Error message - the source contains no data rows

What changes can be done to fix this.
Please help if anyone knows

Thanks

Problem is with CopyToDatatable(). This method only works for collection with at least one row.
To ensure that the method does not return an error you can use if statement with a condition that check if there is at least one row after the filter:

dt1.AsEnumerable().Where(Function (row) Not Dt2.AsEnumerable().Select(Function(r) r.Field(Of String)(“column name”)).Any(Function (x) x=row.Field(of string)(“column name”))).Count()>0

@harshikakhatri269

Instead of CopyToDaTatable first check if count>0 in an if condition if yes then use .copytodatatable else use dt.Clone which will just clone the original datatable with no rows and no error would throw

Cheers

@ppr thanks for the help.
Your solution worked for me.

The error message “the source contains no data rows” in the given VB.NET code usually indicates that the dt1 or dt2 data table is empty or does not contain any data rows.

To fix this issue, you can try the following:

  1. Make sure that dt1 and dt2 are populated with data before running the code. You can check the number of rows in each data table using the Rows.Count property.
  2. Make sure that the column names specified in the Select and Where clauses are correct and exist in the data tables.
  3. Make sure that the data type of the columns specified in the Field(Of String) method is correct. If the data type is not String, you may need to use a different method, such as Field(Of Integer) or Field(Of DateTime), depending on the data type of the column.

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