vb.net “New ... With”语法在 VB Linq 中有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/528610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:03:13  来源:igfitidea点击:

What does the "New ... With" syntax do in VB Linq?

vb.netlinqlinq-to-xml

提问by Steve Davies

What (if any) is the difference between the results of the following two versions of this VB Linq query?

这个 VB Linq 查询的以下两个版本的结果之间(如果有的话)有什么区别?

' assume we have an XElement containing employee details defined somewhere else

' 假设我们有一个包含在其他地方定义的员工详细信息的 XElement

Dim ee = From e In someXML.<Employee> _
Select New With {.Surname = e.<Surname>, .Forename = e.<Forename>}

and

Dim ee = From e In someXML.<Employee> _
Select Surname = .Surname = e.<Surname>, .Forename = e.<Forename>

ie what is the point of the New ... With syntax?

即 New ... With 语法有什么意义?

I suspect that this has a simple answer, but I can't find it - any links to suitable tutorials or Microsoft documentation would be appreciated.

我怀疑这有一个简单的答案,但我找不到它 - 任何指向合适教程或 Microsoft 文档的链接将不胜感激。

采纳答案by Joel Coehoorn

The difference is that the 1st explicitly creates an anonymous type. The 2nd is a query expression, and may use an existing type rather than creating an anonymous type. From the documentation linked by Cameron MacFarland:

不同之处在于第一个显式创建了一个匿名类型。第二个是查询表达式,可以使用现有类型而不是创建匿名类型。来自 Cameron MacFarland 链接的文档:

Query expressions do not always require the creation of anonymous types. When possible, they use an existing type to hold the column data. This occurs when the query returns either whole records from the data source, or only one field from each record.

查询表达式并不总是需要创建匿名类型。在可能的情况下,它们使用现有类型来保存列数据。当查询从数据源返回整条记录,或每条记录仅返回一个字段时,就会发生这种情况。

回答by Andrey Shchekin

My understanding is that there is no difference.

我的理解是没有区别。

New Withis aimed to out-of-query usage like

New With旨在用于查询外使用,例如

Dim X = New With { .Surname = "A", .Forename = "B" }

Specifically for Linq queries, you can skip New With, but it is still useful for other situations. I am not sure, however, since I do not know VB 9 :)

特别是对于 Linq 查询,您可以跳过New With,但它在其他情况下仍然有用。但是,我不确定,因为我不知道 VB 9 :)

回答by JaredPar

There is no functional difference between the two pieces of code you listed. Under the hood both pieces code will use an anonymous type to return the data from the query.

您列出的两段代码之间没有功能差异。在后台,两部分代码都将使用匿名类型从查询中返回数据。

The first piece of code merely makes the use of an anonymous type explicit. The reason this syntax is allowed is that it's possible to return any type from a Select clause. But the type must be used explicitly.

第一段代码只是显式地使用匿名类型。允许使用此语法的原因是可以从 Select 子句返回任何类型。但是必须显式使用该类型。

Dim x = From it in SomeCollection Select New Student With { .Name = it.Name }

Joel is incorrect in his statement that the second query may use an existing type. Without an explicit type, a select clause which uses an explicit property name will always return an anonymous type.

Joel 在他的声明中是不正确的,即第二个查询可能使用现有类型。如果没有显式类型,使用显式属性名称的 select 子句将始终返回匿名类型。

回答by CoderDennis

There is no difference. The compiler will infer the anonymous type.

没有区别。编译器将推断匿名类型。

You most likely want to return the Value of the elements as in e.<Surname>.Value, which returns a String instead of an XElement.

您很可能希望像 in 那样e.<Surname>.Value返回元素的值,它返回一个字符串而不是 XElement。

Your 2nd example could be simplified as

你的第二个例子可以简化为

Dim ee = From e In someXML.<Employee> _
         Select e.<Surname>.Value, e.<Forename>.Value

because the compiler will also infer the names of the members of the anonymous type.

因为编译器还会推断匿名类型成员的名称。

However, if you have the following class

但是,如果您有以下课程

Class Employee

    Private _surname As String
    Public Property Surname() As String
        Get
            Return _surname
        End Get
        Set(ByVal value As String)
            _surname = value
        End Set
    End Property

    Private _forename As String
    Public Property Forename() As String
        Get
            Return _forename
        End Get
        Set(ByVal value As String)
            _forename = value
        End Set
    End Property

End Class

Then you could change the 1st query to produce an IQueryable(Of Employee)instead of the anonymous type by using New ... With like so:

然后,您可以IQueryable(Of Employee)使用 New ...更改第一个查询以生成一个而不是匿名类型,如下所示:

Dim ee = From e In someXML.<Employee> _
         Select New Employee With {.Surname = e.<Surname>.Value, _
                                   .Forename = e.<Forename>.Value}

回答by Cameron MacFarland

They're called Anonymous Types.

它们被称为匿名类型

The main reason for their use is to keep the data from a query in a single object, so the iterators can continue to iterate over a list of objects.

使用它们的主要原因是将查询中的数据保存在单个对象中,因此迭代器可以继续迭代对象列表。

They tend to work as temporary types for storage in the middle of a large or multi-part LINQ query.

它们倾向于在大型或多部分 LINQ 查询中间用作存储的临时类型。

回答by Shawn

One difference is that Anonymous types aren't serializable.

一个区别是匿名类型不可序列化。