An Extensive Examination of LINQ: Using the Query Syntax

Posted by: 4GuysFromRolla.com Headlines, on 15 Apr 2009 | View original | Bookmarked: 0 time(s)

LINQ's standard query operators provide dozens of built-in techniques for programmatically enumerating, aggregating, selecting, and filtering data. These standard query operators are implemented as extension functions on the IEnumerable<T> interface and therefore available to any object that implements this interface, which includes arrays, the collection classes in the System.Collections namespace, and objects that are composed of a set of enumerable items (such as a DataTable). The examples we've explored in this tutorial series thus far have primarily used these standard query operators in their extension method form, namely as methods applied to IEnumerable<T> collections. For example, the following snippet uses this style of syntax to query a collection of Employee objects, returning a list of the employees' Name and Salary for those employees that make more than $100,000 per year, sorted by salary in descending order.

// C# - get the Name and Salary of employees that make more than $100,000, ordered from the highest paid on down
List<Employee> emps = ...;
var SalaryReport = emps.Where( p => p.Salary > 100000M )
.OrderByDescending( p => p.Salary )
.Select( p => new { p.Name, p.Salary } );


' VB - get the Name and Salary of employees that make more than $100,000, ordered from the highest paid on down
Dim emps As List(Of Employee) = ...
Dim SalaryReport = emps.Where(Function(p) p.Salary > 100000) _
.OrderByDescending(Function(p) p.Salary) _
.Select(Function(p) New With {p.Name, p.Salary})

While the above syntax certainly works, it is not the most human-friendly syntax. For starters, it's quite verbose. It's also unlike the data querying syntax most developers are familiar with - SQL. To address these shortcomings, Microsoft added a number of language enhancements to C# 3.0 and Visual 9, which are the versions that shipped with the .NET Framework 3.5 and Visual Studio 2008. In addition to extension methods, implicitly typed variables, object initializers, lambda expressions, and anonymous types, Microsoft also added a new query syntax that allows developers to write LINQ queries in a SQL-like syntax. The following code is semantically equivalent to the example above, but uses the query syntax (or query expressions) instead of the extension method syntax:

// C# - get the Name and Salary of employees that make more than $100,000, ordered from the highest paid on down
List<Employee> emps = ...;
var SalaryReport = from p in emps
where p.Salary > 100000M
orderby p.Salary descending
select new { p.Name, p.Salary } );


' VB - get the Name and Salary of employees that make more than $100,000, ordered from the highest paid on down
Dim emps As List(Of Employee) = ...
Dim SalaryReport = From p In emps _
Where p.Salary > 100000 _
Order By p.Salary _
Select New With {p.Name, p.Salary}

This query syntax is another syntactic sugar language enhancement. When compiled, the above code is translated into calls to the corresponding standard query operators.

This article explores both C# and Visual Basic's query syntax. We'll discuss the mapping between the query syntax and LINQ's standard query operators, as well as see a number of query syntax examples. Read on to learn more!
Read More >

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: XLinq | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1862 | Hits: 31

Similar Posts

  • Introducing Versatile DataSources more
  • Serialising Microsoft StreamInsight QueryTemplates more
  • Chat room questions from the EF Tips & Tricks webcast more
  • An Extensive Examination of LINQ: Grouping and Joining Data more
  • Checking for EF to TSQL Query Compilation Changes in VS2010 Beta1 more
  • An Extensive Examination of LINQ: Lambda Expressions and Anonymous Types more
  • The Twitter Search API made easy with Linq to XML more
  • Create custom LINQ providers fluently more
  • An Extensive Examination of LINQ: Extension Methods, Implicitly Typed Variables, and Object Initializers more
  • An Extensive Examination of LINQ: An Introduction to LINQ more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD