VB.Net与C#中的LINQ语法差异 [英] Differences in LINQ syntax between VB.Net and C#

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

问题描述

一次,只是出于好奇:

在我在VB.Net我已经设定了几个项目让我吃惊的发现,也有一些超过C#和VB.NET使用LINQ之间的细微差别。
为例,如果我们想通过多个属性组元素(列),我们需要明确地创建一个新的匿名类型:

After I have programmed several projects in VB.Net I to my surprise discovered that there are some more than subtle differences between C# and VB.NET LINQ usage. For example, if we want to group elements by multiple properties (columns) we need to create a new anonymous type explicitly:

var procs = from c in Process.GetProcesses() 
            group c by new {c.BasePriority, c.Id} into d 
            select d;



而在VB.NET更直接的语法已经这样做了:

whereas in VB.NET more straightforward syntax will already do:

Dim b = From c In Process.GetProcesses()
        Group c By c.BasePriority, c.Id Into Group
        Select Group

所以,人们并不需要创建一个新这里的类型。

So, one does not need to create a type with "new" here.

有什么其他方面的差异?有没有在C#和VB.NET的LINQ语法之间的比较好?

What are the other differences? Is there any good comparison between the LINQ syntax in C# and VB.NET?

推荐答案

有一些区别,我知道,大部分是VB.NET的LINQ有一些隐藏的宝石:

There are some differences that I know of, mostly that VB.NET's LINQ has some hidden gems:


  1. 未明确LINQ相关,但VB.NET支持匿名类型键修改。这允许你定义在匿名类型属性比较匿名类型时使用。至于我可以用C#告诉;它使用了一切。这是VB.NET有一个实际的好处

  2. VB.NET支持跳过操作关键字:
    暗淡returnCustomers =从在列表中跳过numToSkip选择您可以在C#中做到这一点;但它必须通过扩展方法,没有语法糖

  3. VB.NET LINQ还支持跳过虽然从列表中跳过虽然someCondition选择一个此外,C#可以做到这一点;但只能通过扩展方法

  4. 和4.5:同为2安培。 3除非拍摄虽然

  5. 选择关键字是在VB.NET可选。如果你想选择的是当前的;那么正常工作:暗淡shortWords =从l在列表,其中l.Length< 10 在C#;选择部分是必需的: VAR shortWords =由升的列表,其中l.Length< 10选择L

  1. Not explicitly LINQ related, but VB.NET supports the Key modifier on anonymous types. This allows you to define which properties in the anonymous type are used when comparing anonymous types. As far as I can tell with C#; it uses everything. This is where VB.NET has an actual advantage.
  2. VB.NET supports the Skip operation as a keyword: Dim returnCustomers = From a In list Skip numToSkip Select a You can do this in C#; but it has to be through the extension method, there is no syntactic sugar.
  3. VB.NET LINQ also supports Skip While: From a In list Skip While someCondition Select a Again, C# can do this; but only through the extension method.
  4. and 4.5.: The same as 2 & 3 except with Take and Take While
  5. The Select keyword is optional in VB.NET. If you want to select what is current; then that works fine: Dim shortWords = From l In list Where l.Length < 10 in C#; the Select part is required: var shortWords = from l in list where l.Length < 10 select l

这些都是VB.NET的LINQ的,我知道的更多功能

Those are the additional "features" of VB.NET's LINQ that I am aware of.

例如;用C#:

var skip10 = (from c in customers select c).Skip(10);

和VB.NET中的

Dim skip10 = From c In Customers Skip 10

您可以看到所有这些文档在这里: http://msdn.microsoft.com/ EN-US /库/ ksh7h19t(v = VS.90)的.aspx

You can see the documentation for all of these here: http://msdn.microsoft.com/en-us/library/ksh7h19t(v=VS.90).aspx

这篇关于VB.Net与C#中的LINQ语法差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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