两个VB.NET LINQ组之间的语法差异 [英] Difference Between Two VB.NET LINQ Group By Syntaxes

查看:268
本文介绍了两个VB.NET LINQ组之间的语法差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用LINQ来对项目进行分组,但是我不确定这两种语法之间是否存在差异:

I have been playing around with grouping items using LINQ, and I am not sure if there is a difference between these two syntaxes:

Dim categories1 =
    From p In products Group p By p.Category Into Group,
        TotalUnits = Sum(p.UnitsInStock), ProductCount = Count()
    Select Category, Average = TotalUnits / ProductCount

Dim categories2 =
    From p In products Group p By p.Category Into
        TotalUnits = Sum(p.UnitsInStock), ProductCount = Count()
    Select Category, Average = TotalUnits / ProductCount



第一个执行"Into Group",第二个执行"Into".在这种情况下,组"似乎是关键字而不是组的名称.两种语法似乎产生相同的结果.是否存在这两种语法的原因(例如,一种语法可以在另一种语法不能使用时使用)?第二种语法只是第一种语法的简写吗?

如果您愿意,可以使用以下类和函数来测试上述查询:



In the first, I do "Into Group," and in the second I do "Into". It seems that the "Group" in that case is a keyword rather than the name of the group. Both syntaxes appear to produce the same result. Is there a reason these two syntaxes exist (e.g., can one be used when the other cannot)? Is the second syntax just shorthand for the first?

Here is a class and function you can use to test the above queries, if you like:

Public Class ProductClass
	Public Property Category As String
	Public Property UnitsInStock As Integer
End Class
Public Function GetProductList() As List(Of ProductClass)
	Return New List(Of ProductClass) From {
		New ProductClass With {.Category = "A", .UnitsInStock = 5},
		New ProductClass With {.Category = "A", .UnitsInStock = 3},
		New ProductClass With {.Category = "B", .UnitsInStock = 4}}
End Function

推荐答案

关键字进入组"将添加一个包含所有匹配项的变量.在这种情况下,当您必须使用多个组来收集字段时,将首选进入组".

您可以看到此链接:
http://geekswithblogs.net/WillSmith/archive/2008/05/28/linq-joins-and-groupings.aspx [ ^ ]

在此链接中,作者从两个组中选择了字段,现在您可以了解"Into"和"Into Group"的区别

如果需要将C#转换为VB.Net:
这是转换器 [
The keyword "Into Groups" will add a variable containing all of the matching items. "Into group" will be prefer in that case when you have to use multiple groups to collect fields.

You can see this link:
http://geekswithblogs.net/WillSmith/archive/2008/05/28/linq-joins-and-groupings.aspx[^]

In this link, the author has selected the fields from two groups, now you can understand the difference of "Into" and "Into Group"

If you need converter for C# to VB.Net:
Here is the Converter[^].


这篇关于两个VB.NET LINQ组之间的语法差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆