What is the difference between Equals and = in LINQ?

What is the difference between Equals and = in LINQ?

Dim list As List(Of Foo) = (From a As Foo In FooList _ Join b As Bar In BarList _ On a.Something = b.Something _ Select a).ToList() 

against

 Dim list As List(Of Foo) = (From a As Foo In FooList _ Join b As Bar In BarList _ On a.Something Equals b.Something _ Select a).ToList() 
+4
source share
2 answers

From The Moth , quoted by Matt Warren

"The reason C # has the word" equal "instead of the '== operator was to make it clear that the" on "position you need to put two separate expressions that are compared for equality is not a single predicate expression. Summary maps to the standard Enumerable.Join () a query operator that sets up two separate delegates that are used to compute values ​​that can then be compared. It needs them as separate delegates to build a search table with one and a probe in the search table with the other. A complete query processor, such a The SQL, is free to explore one predicate expression and choose how he will handle this. However, to LINQ works similarly to the SQL required to compound the condition is always specified as an expression tree overhead for simple memory object. "

EDIT

Later in the article.

UPDATE: Vladimir Sadov from the Visual Basic team told me that VB also uses Equals for almost the same reasons.

+5
source

The difference is that the second version will be compiled, and the first will not. VB query syntax requires that the contextual keyword Equals be used when comparing elements in a LINQ query in this way.

How this operator works is defined in section 11.21.5 of the VB language specification.

  • Both expressions should be classified as meaning.
  • Both expressions must reference at least one range variable.
  • One of the expressions must be referenced in the range variable declared in the connection request statement, and this expression must not refer to any other range variables.
  • If the types of the two expressions are not the same, then
    • If the equality operator is specified for two types, both expressions are implicitly converted to it, and it is not an Object, and then converts both expressions to this type.
    • Otherwise, if there is a dominant type for which both expressions can be implicitly converted, then convert both expressions to this type.
    • Otherwise, a compile-time error occurs.
+1
source

Source: https://habr.com/ru/post/1312495/


All Articles