Differences in LINQ syntax between VB.Net and C#

南笙酒味 提交于 2019-11-28 21:28:48

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

  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

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

For example; with C#:

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

And in VB.NET

Dim skip10 = From c In Customers Skip 10

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

Try to look at this:

Visual Basic vs C# LINQ syntax

regards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!