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

Comments are closed