Compare headers of two excel

Hello all I am trying to compare headers of two excel files dt1, dt2.
I am using below LINQ.
dt1.Columns.Cast(Of DataColumn)().Select(Function(c) c.ColumnName).SequenceEqual(dt2.Columns.Cast(Of DataColumn)().Select(Function(c) c.ColumnName))

But getting the error : If: Object reference not set to an instance of an object.

Can you guys check or give any alternative solution?
Data Validation.xaml (9.8 KB)

can you try this

dt1.Columns.Cast(Of DataColumn)().Select(Function(c) c.ColumnName).SequenceEqual(dt2.Columns.Cast(Of DataColumn)().Select(Function(c) c.ColumnName), StringComparer.OrdinalIgnoreCase)

@Dinesh_Babu_S

it will working you can try this if columns match it will output as matched or if not it will give the not matched as output

Xaml : DataValidation.zip (2.1 KB)

for reference you can see the screenshot

@Dinesh_Babu_S

1 Like

Hi @Dinesh_Babu_S

areColumnNamesEqual = dt1.Columns.Cast(Of DataColumn)().Select(Function(c) c.ColumnName).SequenceEqual(dt2.Columns.Cast(Of DataColumn)().Select(Function(c) c.ColumnName))

If areColumnNamesEqual 
Then
    Console.WriteLine("Column names are equal.")
Else
    Console.WriteLine("Column names are not equal.")
End If

Note: areColumnNamesEqual is of DataType Boolean

Hope it helps!!

1 Like

This solution also worked .
Thank you.

1 Like

This solution works if the position of the columns are same in both the Data tabel.
How to Handel if position is changing?

if columns are same and position was different

try this it will work

dt1.Columns.Cast(Of DataColumn)().
    Select(Function(c) c.ColumnName.ToLower()).ToHashSet().SetEquals(
    dt2.Columns.Cast(Of DataColumn)().
    Select(Function(c) c.ColumnName.ToLower()))

@Dinesh_Babu_S

1 Like

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