Jul 26 2007

Apply Method Filtering on Projection Results

Category: DLinq | LinqBil@l @ 14:09

Applying method filtering on the results of a DLinq query is not available. For example, suppose you want to run method named Validate on the Customer ID before returning it in the query:

var result= from c in db.customers
                select new {CustomerName= c.Name, ID= Validate(c.ID)};

This is not allowed, DLinq will not understand the meaning of Validate in the context of the above query expression. What you do is split the above query into two other queries as follows:

var result1 = from c in db.customers
                   select new {CustomerName= c.Name, ID=c.ID};

va result2= from c in result1.AsEnumerable()
                 select new {CustomerName= c.CustomerName, ID= Validate(c.ID)};

Notice the presence of AsEnumerable() which tells DLinq to treat the second query as a local one and not as a query that should be later on processed on the database. Also, keep in mind that, even with the use of AsEnumerable, still the first query is not yet executed!! It is till deferred.

But at least, AsEnumerable allows you to apply your custom methods on the results without having to deal with any problems with DLinq, by simply shifting the processing of the query from the DLinq to a locally executed one!

Hope this helps,

Tags: ,

Jul 25 2007

Is Deferred Execution always required and needed

Category: DLinq | LinqBil@l @ 22:08

In the previous post, I have explained the idea behind Deferred Execution. What if you wanted to directly execute the query expression and bind the results to a GridView or any other Data Control? In this case, Deferred Execution is not required. The solution is simply using one of the methods defined on the Sequence class:

1. ToArray();
2. ToList();

and now the previous query expression is as follows:

var getCustomers= (from c in db.Customers
                             select c).ToList();

We don't have Deferred Execution anymore and the getCustomers now include all the records.

Hope this helps,

Tags: ,

Jul 25 2007

Deffered Execution in DLinq

Category: DLinq | LinqBil@l @ 22:05

I have started recently working on C# 3.0 Enhancements and LINQ project at the same time. One of the nice features of LINQ is the ability to write Query Expressions. You can think of Query Expressions as a high-level SQL Query. You write a query as follows:

var getCustomers= from c in db.Customers
                            select c;

The above is simply a Query Expression that is stored in-memory, and not yet executed! When it is time to execute such a query, it will be retrieved from the memory, translated into a SQL expression, then executed!

If you want to check the results, you have to execute the query right?
To execute a Query Expression, you have to enumerate through the getCustomers query which is of type IQueryable<Customer>.

foreach (var cust in getCustomers)
    Console.WriteLine("Customer ID: {0}", cust.CustomerID);

It is only when you loop through the result-set you are actually executing the query!! This is what is known by Deferred Execution.

Hope this helps,

Tags: ,