VB.NET LINQ中Equals与等号有何不同之处?

2026-05-06 10:232阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计747个文字,预计阅读时间需要3分钟。

VB.NET LINQ中Equals与等号有何不同之处?

LINQ中的Equals和=之间的区别主要在于它们对比较的方式。

1. Equals:这是一个方法,用于比较两个对象的值是否相等。它不仅比较值,还会检查对象的引用是否相同。如果两个对象的引用相同,或者它们的内容相等,则Equals方法返回True。

2. =:这是一个操作符,通常用于比较两个值是否相等。它不检查对象的引用,只比较它们的值。如果两个值相同,则返回True。

以下是一个简化的示例:

vbDim a As New Foo()Dim b As New Foo()Dim c As New Foo()

a.Something=valueb.Something=valuec.Something=value

' 使用EqualsIf a.Equals(b) Then ' a和b的内容相等,但它们可能是不同的对象End If

' 使用=If a=b Then ' a和b的内容相等,并且它们是同一个对象End If

' 使用EqualsIf a.Equals(c) Then ' a和c的内容相等,但它们可能是不同的对象End If

' 使用=If a=c Then ' a和c的内容不相等,因为它们是不同的对象End If

在LINQ查询中,你可以使用这些方法来过滤或连接数据源:

vbDim 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()

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

在这两个查询中,我们都使用了`On a.Something=b.Something`来连接`FooList`和`BarList`。第一个查询使用的是`=`操作符,而第二个查询使用了`Equals`方法。尽管它们在这里看起来相同,但它们的行为是相同的,因为它们都是比较两个值是否相等。

LINQ中Equals和=之间有什么区别?

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()

VB.NET LINQ中Equals与等号有何不同之处?

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() 从 The Moth引用马特沃伦

“The reason C# has the word ‘equals’
instead of the ‘==’ operator was to
make it clear that the ‘on’ clause
needs you to supply two separate
expressions that are compared for
equality not a single predicate
expression. The from-join pattern maps
to the Enumerable.Join() standard
query operator that specifies two
separate delegates that are used to
compute values that can then be
compared. It needs them as separate
delegates in order to build a lookup
table with one and probe into the
lookup table with the other. A full
query processor like SQL is free to
examine a single predicate expression
and choose how it is going to process
it. Yet, to make LINQ operate similar
to SQL would require that the join
condition be always specified as an
expression tree, a significant
overhead for the simple in-memory
object case.”

编辑

后来在文章中.

更新:Visual Basic团队的Vladimir Sadov告诉我,VB也使用Equals,原因几乎相同.

本文共计747个文字,预计阅读时间需要3分钟。

VB.NET LINQ中Equals与等号有何不同之处?

LINQ中的Equals和=之间的区别主要在于它们对比较的方式。

1. Equals:这是一个方法,用于比较两个对象的值是否相等。它不仅比较值,还会检查对象的引用是否相同。如果两个对象的引用相同,或者它们的内容相等,则Equals方法返回True。

2. =:这是一个操作符,通常用于比较两个值是否相等。它不检查对象的引用,只比较它们的值。如果两个值相同,则返回True。

以下是一个简化的示例:

vbDim a As New Foo()Dim b As New Foo()Dim c As New Foo()

a.Something=valueb.Something=valuec.Something=value

' 使用EqualsIf a.Equals(b) Then ' a和b的内容相等,但它们可能是不同的对象End If

' 使用=If a=b Then ' a和b的内容相等,并且它们是同一个对象End If

' 使用EqualsIf a.Equals(c) Then ' a和c的内容相等,但它们可能是不同的对象End If

' 使用=If a=c Then ' a和c的内容不相等,因为它们是不同的对象End If

在LINQ查询中,你可以使用这些方法来过滤或连接数据源:

vbDim 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()

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

在这两个查询中,我们都使用了`On a.Something=b.Something`来连接`FooList`和`BarList`。第一个查询使用的是`=`操作符,而第二个查询使用了`Equals`方法。尽管它们在这里看起来相同,但它们的行为是相同的,因为它们都是比较两个值是否相等。

LINQ中Equals和=之间有什么区别?

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()

VB.NET LINQ中Equals与等号有何不同之处?

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() 从 The Moth引用马特沃伦

“The reason C# has the word ‘equals’
instead of the ‘==’ operator was to
make it clear that the ‘on’ clause
needs you to supply two separate
expressions that are compared for
equality not a single predicate
expression. The from-join pattern maps
to the Enumerable.Join() standard
query operator that specifies two
separate delegates that are used to
compute values that can then be
compared. It needs them as separate
delegates in order to build a lookup
table with one and probe into the
lookup table with the other. A full
query processor like SQL is free to
examine a single predicate expression
and choose how it is going to process
it. Yet, to make LINQ operate similar
to SQL would require that the join
condition be always specified as an
expression tree, a significant
overhead for the simple in-memory
object case.”

编辑

后来在文章中.

更新:Visual Basic团队的Vladimir Sadov告诉我,VB也使用Equals,原因几乎相同.