15

Again, just out of curiosity:

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;

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.

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

Community
  • 1
  • 1
Alexander Galkin
  • 12,086
  • 12
  • 63
  • 115

2 Answers2

18

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

vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • 1
    So, if I understand you right, there are more LINQ extension methods in VB.Net can be written using operator-like syntax compared to C# (like instead of writing `(...).Skip()` one writes just `Skip` in the middle of the LINQ query. That's nice, thank you! – Alexander Galkin Jun 29 '11 at 02:33
  • 1
    @Alaudo - yes, it appears that VB.NET just has more of it baked into the actual language itself. I also added #5 which indicates that the `Select` part isn't mandatory like it is in C#. – vcsjones Jun 29 '11 at 02:36
  • Oh, thank you for adding some more examples! As regards C# and LINQ, as far as I know you can skip `select` too, like `var d = from c in customers where c.Age > 18`. Am I wrong? – Alexander Galkin Jun 29 '11 at 02:37
  • @Alaudo: No. That will result in this compiler error: `A query body must end with a select clause or a group clause`. The error is documented here: http://msdn.microsoft.com/en-us/library/bb383932(v=vs.90).aspx – vcsjones Jun 29 '11 at 02:55
  • 1
    There is also `Aggregate`, but that [has its own problems](http://stackoverflow.com/q/12264751/256431). – Mark Hurd Aug 17 '13 at 00:54
  • As far as I can tell, the answer is still true as of April 2022. Has Microsoft (or anyone else) given a rationale why C# lacks some of the convenient sytnax available in VB.NET? – Quirin F. Schroll Apr 29 '22 at 08:19
2

Try to look at this:

Visual Basic vs C# LINQ syntax

regards

BizApps
  • 6,048
  • 9
  • 40
  • 62
  • 1
    I am primarily interested in differences as concerns LINQ, the first link is interesting but very short -- it is just a block entry. Two remaining ones are outdated comparisons of VB.Net vs. C# and do not include any information on LINQ. Pity... – Alexander Galkin Jun 29 '11 at 02:25