DCSIMG
Introducing to DLINQ – How to write the queries - part 2 - Wortzel's blog

Wortzel's blog

.Net (2.0, 3.0, 3.5), C#, Asp.net, Com+, GIS(ESRI Software), Management, Analysis & Design, Life, Trips, And more...
Introducing to DLINQ – How to write the queries - part 2

In this post I'll focus on the query options in DLINQ.

Let's take two simple scenarios:

1. Retrieve all the employees with the letter "a" in their first name.

2. Retrieve all employees in the "R&D" department

 

There are three common ways to write these queries in DLINQ:

      1. Using SQL syntax:

The most simple (or maybe intuitive) way to write a standard query statement is by using the SQL syntax.

For example, for the first scenario we should write a code like this:

IEnumerable<Employee> list = from x in db.Employees

                                where x.Name.Contains("a")

                             select x;

 

For the second scenario we should write this one:

IEnumerable<Employee> list =

from x in db.Employees

            where x.Department.Description.Equals("R&D")

         select x;

 

2. Using lambda expression:

Lambda expression is a new feature in framework 3.0 which gives us a new friendly and concisely syntax for anonymous delegates.

And now the first query will look like this:

IEnumerable<Employee> list = db.Employees.Where(

      x => x.Name.Contains("a"));

  

And the second one will look like this:

IEnumerable<Employee> list = db.Employees.Where(

x => x.Department.Description.Equals("R&D"));

 

3. Using anonymous delegates:

This isn't a common method but you can still write the query using the Anonymous delegates (one of the new .Net 2.0 feature).

The same query but now with anonymous delegate:

   IEnumerable<Employee> list = db.Employees.Where(

      delegate(Employee emp)

      {

            return emp.Name.Contains("a");

   });

And the second one:

IEnumerable<Employee> list = db.Employees.Where(

      delegate(Employee emp)

      {

            return emp.Department.Description.Equals("R&D");

   });

 

Notes:

1. If we'll look on the compile code of the sql syntax and the lambda expression in reflector we'll get exactly the same thing.

2. The two first options (sql syntax & Lambda expression) are using an expression tree that really improves the application performance. We don't need to bring all the table rows when we need just a few of them. In run time the lambda expression will integrate to a one sql statement. But on the third case, (when we are using anonymous delegates) it will always bring all the data and after we performed some operation (GetEnumerator() method for example) on the query the anonymous delegates will execute for all the result records.

 

Read more:

Introducing to DLINQ

·   Introducing to DLINQ - Entities declaration - part 1

·   Introducing to DLINQ – How to write the queries - part 2

·   Introducing to DLINQ – How to write data manipulation in DLINQ (Update, insert and delete) - part 3

Advanced topics in DLINQ:

·   DLINQ – Advanced topics – Transaction support – part 4

·   DLINQ – Advanced topics – How it works in ASP.NET application – part 5

·   DLINQ – Advanced topics – Debug mode – part 6

 

Published Thursday, August 09, 2007 1:33 AM by Avi Wortzel

Comments

No Comments