LINQ Syntax

9/24/2005 12:23:12 PM

LINQ Syntax

Query expressions in C# 3.0 bear much more resemblance to XQuery than to SQL in many ways. Below is a sample XML query using XQuery’s FLWOR syntax.

for $d in document("depts.xml")//deptno
let $e := document("emps.xml")//employee[deptno = $d]
where count($e) >= 10
order by avg($e/salary) descending
return
  <big-dept>
     { $d,
        <headcount>{count($e)}</headcount>,
        <avgsal>{avg($e/salary)}</avgsal>
     }
  </big-dept>

The translation to C# 3.0 and XLINQ is remarkably similar, demonstrating that LINQ is a complete approach to expressing queries:

from d in XElement.Load("depts.xml").Descendants(“deptno”),
  e = XElement.Load("emps.xml").Descendants(“employee”)
     .Where( x => x.Element("deptno").Value == d.Value ) where e.Count() >= 10 orderby e.Element(“salary”).Average() descending select new XElement(“big-dept”,        d,        new XElement(“headcount”, e.Count()),        new XElement(“avgsal”, e.Element(“salary”).Average()));

Some observations:

  • The LET keyword of XQuery is translated to C#’s from assignment, which is not documented in the currently published draft specification of C# 3.0.
  • From a recent chat on Sept 22, the C# team is still exploring the possibility of incorporating XML literals, which would make the XQuery resemblance even more stunning. Also reported was the possibility of DML operations (insert, delete, and update).
  • C# syntax from-select notation appears better suited than VB’s select-from notation when large XML fragments or large object initializers are selected, because related parts of queries are kept close together. In VB’s case, the select clause and its corresponding from clause may not even be in the same page or screen; in addition, any embedded query expressions within the select clause could potentially cause confusion. In addition, C#’s notation corresponds to the actual order of operations.
  • VB’s syntax adheres more closely to SQL according to features announcements. (The VB team has warned that some features aren’t yet implemented in the current preview release.) VB includes most, if not all, of SQL’s operators and functions. For instance, anonymous types can constructed implicitly in the SELECT clause (not possible with C#'s FROM/SELECT notation). Other operators supports include different join types, aggregation functions.
  • Certain database-specific features such as stored procedures are possibly to encode as function calls in queries that take expression trees.

Orcas development is still early in development. The C# team is somewhat tightlipped about upcoming enhancements (as in the Whidbey release), whereas the VB team seems more forthcoming about feature possibilities being explored.

Comments

 

Navigation

Categories

About

Net Undocumented is a blog about the internals of .NET including Xamarin implementations. Other topics include managed and web languages (C#, C++, Javascript), computer science theory, software engineering and software entrepreneurship.

Social Media