w0051977
w0051977

Reputation: 15787

Linq query takes far too long

Please see the code below:

 Dim listPersonExact As List(Of type1) = ISystem1.GetMatches(getExclusions()) '1    
        Dim checks = From t1 In listPersonExact _
            Join t2 In listCheckedMerges On t1.MasterID Equals t2.MasterID And t1.ChildID Equals t2.ChildID _
            Select New With {t1.MasterID, t1.ChildID, t1.MergeTypeID}
        Dim listCheckedMerges As List(Of type1) = ISystem1.GetMatches


        For Each var In checks
            MsgBox('Got here')
        Next

The ForEach is reached very quickily. However, it takes a very long time to move from For Each to the message box. Is there any way I can optimise this?

Update Following on from Tim Shmelters comment I have written the following as a test:

Public Shared Sub LinqTest()
        Dim t1 As New TestPerson
        t1.id = 1
        t1.name = "Ian"

        Dim t2 As New TestPerson
        t2.id = 2
        t2.name = "Lauren"

        Dim list As List(Of TestPerson) = New List(Of TestPerson)
        list.Add(t1)
        list.Add(t2)

        Dim list2 As List(Of TestPerson) = New List(Of TestPerson)
        list2.Add(t1)

        Dim test = From p1 In list Join p2 In list2 On New With {p1.id, p1.name} Equals New With {p2.id, p2.name} Where t1.id = t2.id And t1.name = t2.name Select p1.id, p1.name
        For Each var In test
            MsgBox(var.id)
            MsgBox(var.name)
        Next


    End Sub

I would expect the FOR loop to loop once, however it does not loop. Why is this?

Upvotes: 1

Views: 103

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460058

The ForEach is reached very quickily. However, it takes a very long time to move from For Each to the message box.

That's LINQ's deferred execution. The query itself just defines how it will be executed. The For Each will actually execute it.

But it should not compile at all. You can't use And in the join condition. Use an anonymous type if you want to join on multiple columns/properties.


According to this hint you've tried following:

Dim test = From p1 In list Join p2 In list2 
           On New With {p1.id, p1.name} Equals New With {p2.id, p2.name} 
           Where t1.id = t2.id And t1.name = t2.name 
           Select p1.id, p1.name

I would expect the FOR loop to loop once, however it does not loop. Why is this?

In VB.NET you have to define the keys of an anonymous type (as opposed to C# where all properties are the keys automatically). Otherwise only references are compared.

So this should work(note that your Where is redundant):

Dim test = From p1 In list Join p2 In list2 
           On New With {Key p1.id, Key p1.name} Equals New With {Key p2.id, Key p2.name}
           Select New With {Key p1.id, Key p1.name}

Upvotes: 1

Related Questions