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,
Regards 

Tags: ,

Jul 26 2007

Deferred Loading in DLinq

Category: DLinqBil@l @ 14:08

Suppose we have an entity class called Customer and it contains a property of type List of Order entity classes. Now we want to retrieve all the customers and their orders, how do we do this using Query Expression?

var results=  from c in db.Customer
  select c;

However, this will not load the orders inside the customer entities returned! Why? The reason is that DLinq uses deferred loading and is known as "lazy loading".
In the above query, we only retrieved the customer entities, but the Orders were not loaded too! This goes back to the fact that Customer class has a relation with the Order class just as the relation between Customer and Order data tables. in the database. This explains that related objects are governed by "Deferred loading". Only the main class's properties are loaded, and all related objects are not.

But, when you enumerate through the results, and when you access the Orders property, DLinq goes to the database and retrieves them without letting you know of this!

So, what if we wanted to disable Deferred Loading? You could do a projection and retrieve both elements, but then the result of the projection will be read only. This is where we use DataShape object!!

 DataShape ds = new DataShape();
 ds.LoadWith<Customer>(c => c.Orders);
 db.Shape = ds;
 var q =
  from c in db.Customers
  where c.City == "London"
  select c;

You are informing the query to load the Orders collection on the Customer class, whenever a customer entity is loaded from the database!

Hope this helps,
Regards

Tags:

Jul 25 2007

Object Identity in DLinq

Category: DLinqBil@l @ 22:31

I have found out that DLinq internally uses Object Pooling!

Usually, when you retrieve a record from a Database Table, and then retrieve it again, another version is being created for you. This doesn't hold true for C# or VB.NET objects. When  you create an instance of an object, and then request it again, the same instance is brought back (for sure if GC didn't remove it).

With DLinq, when a record is retrieved from the database it is bound or linked to a C#/VB.NET object, its Primary Key is used to store that instance of the object created. So that next time, when you request same Row from the database, a check is made into the store of the "objects pooled" against instance that is mapped to the request Row based on the Primary Key, if the instance is present, then it is retrieved and returned back, else a new instance of the object is created by accessing the database.

Off course, when an update is made to a row, the instance is removed from the pool to maintain integrity of data!

That is really cool!
Regards

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,
Regards

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,
Regards

Tags: ,