tags:

views:

82

answers:

2

Are the below two queries functionally the same? The first one doesn't return any data, but the second works fine with same exact input. Can someone point out what's wrong in my first query?

Dim LTest2 As IEnumerable = From e1 As QNCEntity In Shape.Entities _
                        Join e2 As QNCEntity In Shape.Entities _
                        On New With { _
                                     .X = CDbl(e1.EntObj.X2), _
                                     .Y = CDbl(e1.EntObj.Y2) _
                                    } _
                        Equals New With { _
                                           .X = CDbl(e2.EntObj.X1), _
                                           .Y = CDbl(e2.EntObj.Y1) _
                                        }

Dim LTest3 As IEnumerable = From e1 As QNCEntity In Shape.Entities _
                               Join e2 As QNCEntity In Shape.Entities _
                               On CDbl(e1.EntObj.X2) Equals CDbl(e2.EntObj.X1) _
                               And CDbl(e1.EntObj.Y2) Equals CDbl(e2.EntObj.Y1)

Thanks

A: 

Is Width a class? Yes? In this case the first query will compare the objects by reference while the second one will compare the actual values. (... and the references will - of course - never match because you always create a new instance.)

Daniel Brückner
"New With" is a VB.NET idiom for creating anonymous objects
ichiban
Good to know (I am C# developer) ... I assumed a typo. In this case I am completly wrong.
Daniel Brückner
A: 

An instance of an anonymous type with no key properties is Equal only to itself.

See: MSDN article on Anonymous Types, Header - Key Properties, Equality

To make the first query work the same as the second, you need to change your first code sample to mark the X and Y properties as key properties:

Dim LTest2 As IEnumerable = From e1 As QNCEntity In Shape.Entities _
                        Join e2 As QNCEntity In Shape.Entities _
                        On New With { _
                                      Key .X = CDbl(e1.EntObj.X2), _
                                      Key .Y = CDbl(e1.EntObj.Y2) _
                                    } _
                        Equals New With { _
                                          Key .X = CDbl(e2.EntObj.X1), _
                                          Key .Y = CDbl(e2.EntObj.Y1) _
                                        }

The key fields will be compared by value for an equality test instead of comparing the references to the two anonymous types.

HipCzeck
Thanks that works.
Nev_Rahd