1

I have the following function:

Public Function Check_Desparity(Byval dtTestStep as DataTable, Byval dtLimits as DataTable) as DataTable

Dim diff = dtTestSteps.AsEnumerable.Union(dtLimits.AsEnumberable).Except(dtTestSteps.Intersect(dtLimits.AsEnumerable))

End Function

I expect, that diff contains the rows with differences. But it doesn`t. I have two differences, but diff contains only one and that one is no difference.

When I try the same thing with List(Of String) instead of DataTable it works perfect.

Public Function Check_Desparity(Byval TestStep as List(Of String), Byval Limits as List(Of String)) as List(Of String)

    Dim diff = TestStep.Union(Limits).Except(TestStep.Intersect(Limits))

    End Function

Here I get exactly the two differences of both lists back in diff.

Could somebody explain me why? Thank you

EDIT:

With help of you, I got exactly what I wanted. The function for my answer is the following:

Public Function Check_Desparity(Byval dtTestStep as DataTable, Byval dtLimits as DataTable) as IEnumerable(Of DataRow)

    Dim diff = dtLimits.AsEnumerable.Except(dtTestSteps.AsEnumberable, DataRowComparer.Default)

    Return diff

    End Function

But I forgot to mention an important detail.

This function works only if both of the tables have the same columns.

In my case, the columns are different, but column "dictkey". Column "dictkey" exists in both of my datatables.

How I get it to work, that my function returns only rows, where "dictkey" is different respectivly not existent?

2 Answers2

3

You can't use Except, Intersect or Union in this way because DataRow.Equals is not overridden, hence it will just compare references and all are different. You can use DataRowComparer.Default which compares all columns of the row with all columns of the other row.

Your LINQ query doesn't make sense either, i guess you want something like this:

Public Function Check_Desparity(ByVal dtTestStep As DataTable, ByVal dtLimits As DataTable) As DataTable
    Dim stepRows = dtTestStep.AsEnumerable()
    Dim limitRows = dtLimits.AsEnumerable()

    Dim allInStepButNotInLimit = stepRows.Except(limitRows, DataRowComparer.Default)
    Dim allInLimitButNotInStep = limitRows.Except(stepRows, DataRowComparer.Default)
End Function 
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • If i want to return allInLimitButNotInStep, which return value I would have in this case? – Manfred Singer Feb 16 '18 at 10:27
  • Sorry. I got it. As IEnumerable(Of DataRow) – Manfred Singer Feb 16 '18 at 10:32
  • But it seems, that this is only working, if each of the datatables contains only one column. Is there any possibility to check desparity only for a specific column? – Manfred Singer Feb 16 '18 at 10:39
  • @ManfredSinger: it works also for multiple columns, it will compare all columns, so the order, the type and the value are taken into account. – Tim Schmelter Feb 16 '18 at 10:40
  • You are right. But my problem is, that the datatables have different columns. I want to check only the column "dictkey" which is in both columns. The other columns are different. – Manfred Singer Feb 16 '18 at 10:42
  • 1
    You should have mentioned that first. How we should know it? You could either use a custom `IEqualityComparer(Of DataRow)`(a class which implements that) or use `GroupBy`. But edit your question first or better ask a new with these informations. – Tim Schmelter Feb 16 '18 at 10:43
  • I forgot this to mention. Sorry. Is there a possibility to do this in that way? – Manfred Singer Feb 16 '18 at 10:43
  • Because of the time-restrictions regarding questions, I prefered to edit my question instead of writing a new one. – Manfred Singer Feb 16 '18 at 11:07
1

I think this is because DataTable.AsEnumerable returns IEnumerable of DataRow.

DataRow is a reference type and since LINQ uses Equals() for comparison to find differences, all rows from both tables are considered to be different (they all are different objects).

Your code works for strings because they are compared using their content, like value types.

the_joric
  • 11,986
  • 6
  • 36
  • 57
  • I saw this post https://stackoverflow.com/questions/9289475/difference-of-two-datatables-in-c-sharp, but it doesn't work in my case, even when I try it with the same syntax as described in the posting. – Manfred Singer Feb 16 '18 at 09:57