Sunday, September 20, 2009

Language Intregrated Query (LINQ)

At the heart of LINQ is a framework defined under the Systme.LINQ namespace, which when coupled with the new language constructs of C# 3.0 provides the ability to write LINQ queries, either over objects held locally or over remote data sources.


The key features of C# 3.0 that are important to writing LINQ queries are extension methods and lambda expressions.
  • Extension methods enable you to extend the methods of an existing type. For example, you could create an extension method to the String class called ToStringArray(), which when called turns a string variable into a string array.
  • Lambda expressions enable you to write an expression within a method signature that is evaluated at runtime. Lambda expressions are a shorthand way of writing a delegate.


  • LINQ provides classes that contain extension methods commonly referred to as query operators.
Examples of LINQ query operators are Select, Where, and GroupBy. Many of these methods will feel familiar to SQL developers as much of the set- based logic in LINQ follows the same semantics as SQL. Query operators accept lambda expressions as a way of evaluating the input to the query operator to return an output.


LINQ to Local Data


LINQ defines a class named Enumerable, which contains the query operators used to write LINQ queries over local objects in memory. These query operators are extension methods to the IEnumberable interface, which is an existing .NET interface inherited by all generic collections, such as List.
  • Using extension methods in this way means that any type that inherits IEnumberable can be queried using LINQ.
  • Objects that implement IEnumberable are collections of objects or XML documents loaded into memory.
These two flavors of LINQ are commonly referred to as LINQ to Objects and LINQ to XML, respectively.


Query operators defined in the Enumberable class typically accept an input collection and return an output collection. The contents of the output collection would depend on the conditions of the Lambda expression.


A simple LINQ query is shown using Extension Methods and Lambda Expressions
---------------------------------------------------------------------------------------------------------
using System;
using System.Linq;


namespace ME


{
      class Program
      {
              static void Main(String[] args)
              {
                      sting[] names = new string[] { "Simon", "Johe", "Angharad", Tim" };
                      string[] longnams = GetLongNames(names);


                      foreach(string name in lognames)
                     {
                               Console.WriteLine(name);
                     }


                     static string[] GetLongNames(string[] names)
                     {
                               var longnams = names.Where(n => n.length > 4);
                               return longnames.ToArray();
                     }
             }
}
-------------------------------------------------------------------------------------------------------------
Executing this code would return two names: Simon and Angharad.
  • The Where() method is a query operator defined in Enumerable.
  • The lambda expression it executes returns an output collection of string objects longer than four characters. The n variable is of type string because the array names is a string array.
The Where() query operator is able to be executed directly from the names string array, because the string array inherits IEnumerable.


Notice that the object longnames is declared as type var.
  • The var keyword in C# 3.0 is an implicit type. This means that it is strongly typed, taking the return type of the Where() method, which is the output collection of type IEnumerable.
Finally, the query operator ToArray() is called to cast the output sequence to the type string[]. This is an important step in the preceding code, because it is only here that the query is actually executed; this process is known as deferred execution, which can mislead developers who think that the result of the query is set on the line of code that returns the original output sequence.


LINQ to Remote Data


LINQ defines a class named Queryable, which contains the query operators used to write LINQ queries to remote data. These query operators are extension methods to the IQueryable interface, which is an interface inherited by collections that represent the remote data source.
  • The query operators under the Queryable class largely mirror those in the Enumerable class, meaning that to developers, the syntax for writing LINQ queries is the same whether you are querying local objects or remote data.
The IQueryable() objects that represent the underlying data source are objects provided by a LINQ implementation.


The two LINQ implementations that come out the box with the .NET Framework are LINQ to SQL and LINQ to Entities.
  • LINQ to SQL translates LINQ queries into Transact SQL (T- SQL) for SQL Server.
  • LINQ to Entities translate a LINQ query into Entity SQL (E- SQL) for the ADO.NET Entity Framework. E- SQL is a special form of SQL used by the ADO.NET Entity Framework to query the conceptual model defined by the ORM(Object Relatational Mapping).
The IQueryable classes generated either by using LINQ to SQL or LINQ to Entities are wrapped in a context class (named DataContext for LINQ to SQL or ObjectContext for LINQ to Entities). It is this class that is wired into the ADO.NET Data Services DataService service implementation, where T is your strongly typed context object.

LINQ TO SQL VS. LINQ TO ENTITIES

The key difference between LINQ to SQL and LINQ to Entities is that LINQ to Entities provides a conceptual model for your database, whereas LINQ to SQL works much more closely with the relational data model of your database.

The extra layer of abstraction LINQ to Entities provides from the structure of the database itself enables you to design an object- oriented representation (conceptual model) of your relational database. Queries written in LINQ to Entities are executed against this conceptual model using E- SQL. The entity framework handles mapping between the conceptual model of your entities and the relational model of database using an ORM(Object Relatational Mapping), thus addressing an impedance mismatch that can occur between how you ideally want to model the data in the database and how you want represent an object model in your code.


LINQ to SQL has limited mapping capabilities and unlike LINQ to Entities only works with SQL Server. However, if you do not have a big impedance mismatch between your database design and object model  using SQL Server, LINQ to SQL is a simpler and therefore quicker option to use for development.

No comments:

View Tenant (ULS) Logs in SharePoint Online using CSOM

Even though the classes exist in the CSOM, the Microsoft Office 365 Engineering team has confirmed that this is something which is not poss...