Feed

In my last blog post I wrote a quick introduction on the Entity Framework. I've been playing with it for the last couple days. I must say I had a bit of a hard time trying to make it work. The good part is that I discovered some nice stuff when searching for solutions.

With Linq, you are able to build a query predicate dinamically with the extension method IQueryable<TSource> Where<TSource>:

var queryResults = from p in db.Products;

                            select p;

queryResults = queryResults.Where(p => p.Price < 1000);

 But what happens if later your business logic finds out that you need to include in your results products which also cost more than $3000?

 No problem! Just call "Where" again:

(...)

queryResults = queryResults.Where(p => p.Price > 3000);

Right? NO! This would result in a query that filters the products for Price < 1000 AND Price > 3000. That means, you won't get any results at all! :) What you need, is an OR operator between your two Where predicates.

The solution for this is the LinqKit! It comes with a neat pack of tools for Linq, including a PredicateBuilder with some extension method that allow you to do something like:

(...)

var queryResults = from p in db.Products;

                            select p;

var predicate = PredicateBuilder.False<Product>();
predicate = predicate.Or(p => p.Price < 1000);
predicate = predicate.Or(p => p.Price > 3000);
queryResults = queryResults.AsExpandable().Where(predicate);  // p.Price < 1000 OR p.Price > 3000 

 You must have noticed the AsExpandable() call above. As per LinqKit documentation:

"Entity Framework's query processing pipeline cannot handle invocation expressions, which is why you need to call AsExpandable on the first object in the query. By calling AsExpandable, you activate LINQKit's expression visitor class which substitutes invocation expressions with simpler constructs that Entity Framework can understand."

The Entity Framework has some limitations when compared with Linq to SQL so that some method are not supported. You can check out that is supported and what is not here.

If one attempts to use any of these unsupported methods, a runtime exception will be thrown.

 

 

PS: Thanks to this blog post, I was able to write nice formatted C# source code in this post.

Microsoft has been working on the next version of its .Net Framework 4.0 and the respective IDE: Visual Studio 2010. There is a video screencast at Channel9 for downloading and installing the Beta 1.

Some of the new features in this version 4 are: parallel programming support, functional programming with F#, code contracts, new WF engine, etc.

One of the biggest improvements I found very useful was the ASP.NET Dynamic Data framework, which was introduced in .Net 3.5 SP1. With this tool, you are able to create an entire data driven website in minutes. It uses Linq to SQL to generate the object/relational data mapping and then builds up pages based on columns data types. There are several built-in Field Templates and Page Templates that are used to build the pages dynamically based on the table schema. Fantastic!

The Dynamic Data Preview 4 can be downloaded from CodePlex.

However, Microsoft also introduced the ADO.Net Entity Framework (or Linq to Entities) in .Net 3.5 SP1 as well, that  seems to do the same job that L2S proposes to do. There are some discussions in the web around this. This blog post outlines the main differences between the two technologies. which basically tells us that L2S is more limited that the Entity Framework.

Finally, "... as of .NET 4.0, LINQ to Entities will be the recommended data access solution for LINQ to relational scenarios." (source)

 

My first blog post

by Felipe Lima 19/05/2009 21:00

I've been lately playing with this blog, customizing preparing the floor for this first post. I've used for this, the BlogEngine.NET, which seems to be very powerfull and versatile.

I opted for a simple and clean dark look instead of a heavy fractal background image and transparency on posts, which was pretty cool, but bad for reading. For the title gradient look, I was inspired by a nice post in Tutorial9 website, which has some good web design tips and tutorials. Check out.

 The intent of this blog is to register and share my findigs on any topic, language and subject that is related to programming and software development in a general way. I'll post my findings as soon as I have something new and useful to post!

My interests are .NET Framework development, specially web-based (ASP.NET) with C#; Javascript, HTML, CSS, Flash Development (ActionScript) and so forth.

 So, to officially open this blog, I start with a quick z-axis CSS trick that I found out lately:

In order to be able to specify a z-index to an element in an HTML page, that element must be absolutely positioned (position: absolute), as per w3-schools. You can achieve this by setting only your inner-most div to position:absolute, since absolute positioned elements are not repositioned when the window is resized, so it would be probably useful to put it inside a centered div in order to not to compromise the whole page appearance.

Tags: , ,

About the Author

myself
My name is Felipe Lima and I am a Software Engineer and co-founder at Quavio, a digital agency at Porto Alegre, Brazil. I am interested in technology, programming with the .Net Framework and sports. Check out my full profile.

Powered by BlogEngine.NET 1.4.5.0