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?
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.
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.
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.
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.
There are some differences that I know of, mostly that VB.NET's LINQ has some hidden gems:
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.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.Skip While
: From a In list Skip While someCondition Select a
Again, C# can do this; but only through the extension method.Take
and Take While
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With