Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Differences in LINQ syntax between VB.Net and C#

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?

like image 503
Alexander Galkin Avatar asked Jun 29 '11 02:06

Alexander Galkin


People also ask

Can you use LINQ in VB net?

The Language Integrated Query (LINQ), which is pronounced as “link”, was introduced in the . NET Framework 3.5 to provide query capabilities by defining standardized query syntax in the . NET programming languages (such as C# and VB.NET). LINQ is provided via the System.

What is the difference between query syntax and method syntax in LINQ?

Query syntax and method syntax are semantically identical, but many people find query syntax simpler and easier to read. Some queries must be expressed as method calls. For example, you must use a method call to express a query that retrieves the number of elements that match a specified condition.

What is LINQ query syntax?

LINQ query syntax is consist of a set of query keywords defined into the . NET Framework version 3.5 or Higher. This allows the programmer or developers to write the commands similar to SQL style in the code(C# or VB.NET) without using quotes. It is also know as the Query Expression Syntax.

What is missing LINQ in VB net?

This means that you can use LINQ to query data sources such as ADO.NET DataSets or SQL Server tables and views. But LINQ can query a lot more. XML also “has structure”. LINQ allows queries against any XML data source including an XML file or an XML string in memory.


2 Answers

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

like image 57
vcsjones Avatar answered Oct 05 '22 06:10

vcsjones


Try to look at this:

Visual Basic vs C# LINQ syntax

regards

like image 39
BizApps Avatar answered Oct 05 '22 07:10

BizApps