Alessandro Del Sole's Blog

{ A programming space about Microsoft® .NET® }

  Home :: Contact :: Syndication  :: Login
  73 Posts :: 10 Stories :: 141 Comments :: 1729 Trackbacks

News

Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2010

Order my book about VB 2010 on Amazon My book "Visual Basic 2010 Unleashed" is available. Click the cover!

Your visits

Windows Live Alerts

Vsi Builder 2008

My tool for VS 2005/2008 Download Vsi Builder, my dev tool for Visual Studio 2005/2008!

Add me to MSDN Social!

Follow me on Twitter!

Messenger me!


Grab this badge here!

CyberInstaller Beta Tester

Download CIS 2008!!

CodePlex download Download my open-source projects from CodePlex!

Search the blog



Article Categories

Archives

Post Categories

.NET Framework

Blogroll

Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

In a previous post we discussed about creating data forms for showing master-details data representations with Visual Basic 2008 and the ADO.NET Entity Framework and we said that using strings within the eager loading technique could cause the loss of the benefits provided by the LINQ strongly typed approach.

 

I then found a blog post by Matthieu Mezil (Microsoft Visual C# MVP), who solved the problem implementing a custom Include method using lambda expressions instead of strings, taking the advantage of the strongly typed approach. Matthieu's code is in Visual C# with his help we converted the code into Visual Basic 2008.

 

The code is quite short, but it requires a good knowledge of LINQ, so you'll find links to the MSDN library for each object we will utilize.

 

Let's retake the demo application we saw in the previous post then let's add a new module. In this module we must first add the following Imports directives:

 

Imports System.Runtime.CompilerServices

Imports System.Data.Objects

Imports System.Linq.Expressions

Imports System.Reflection

 

Once we've done this, let's type the following code which implements an extension version of the Include method. This is the code about the new module and that we'll comment below:

 

<Extension()> Module ObjectQueryExtension

 

    <Extension()> _

    Function Include(Of T)(ByVal MainQuery As ObjectQuery(Of T), _

                           ByVal SubSelector As Expression(Of Func(Of T, Object))) _

                           As ObjectQuery(Of T)

 

        Return MainQuery.Include(CType(CType(CType(SubSelector.Body,  _

                                 UnaryExpression).Operand,  _

                                 MemberExpression).Member,  _

                                 PropertyInfo).Name)

    End Function

End Module

 

Extension methods must be adorned with the Extension attribute and must be exposed by modules adorned with the same attribute. The Include method requires a generic type Of T. The first argument the method receives is of type ObjectQuery(Of T), that represents, in a strongly typed way, a query executed versus the Entity Data Model. In our case the ObjectQuery is constituted by the NorthwindContext.Categories object. The secund argument is of type Expression (Of TDelegate), and is a strongly typed representation of a lambda expression that is structured as an expression tree.

The method body invokes the Include method versus the ObjectQuery instance that it received (in our sample it's always the NorthwindContext.Categories).

The lambda expression body is converted into an UnaryExpression. The result of this expression (Operand) is then converted into a MemberExpression, which is the infrastructure for accessing fields or properties which are then accessed via the reflection.

 

Invoking this method is quite easy. If we would apply the method to the demo application we saw in the previous post, we can replace the line of code for the eager loading as follows: 

 

        'Dim dataSource = NorthwindContext.Categories.Include("Products")

 

        Dim dataSource = NorthwindContext.Categories.Include(Function(c) c.Products)

 

You can surely notice the difference. We are no more passing a string, because we are now passing a lambda expression that allows us working in a strongly typed way (with many advantages from this). The local type inference correctly assigns the Categories type to the c object and the ObjectQuery(Of Categories) type to the dataSource one.

 

If you try to run the application you'll obtain the same result of the previous time, but this time we retrieved data with a more efficient and "managed" approach.

 

Alessandro

posted on Monday, January 19, 2009 10:59 PM

Feedback

#  &raquo; LinQ to Entities on ADO.Net vs LinQ to SQL Florent Clairambault 10/4/2009 10:50 PM Pingback/TrackBack
&raquo; LinQ to Entities on ADO.Net vs LinQ to SQL Florent Clairambault

# re: Strongly typed eager loading in Entity Framework with Visual Basic 1/27/2010 9:23 PM Lance
Awesome idea -- hate using the string when doing an include -- is there a possibility that you can translate this into C#? Something in the conversion that I'm trying isn't working correct. Thanks.

# re: Strongly typed eager loading in Entity Framework with Visual Basic 1/27/2010 9:45 PM Alessandro Del Sole
Hi Lance, this is the blog post from Matthew that I mention in the post above:
http://msmvps.com/blogs/matthieu/archive/2008/06/06/entityframework-include-with-func.aspx

This is the original C# version. Thanks.

# re: Strongly typed eager loading in Entity Framework with Visual Basic 7/27/2010 8:24 AM latex mattress
This is a very useful post! I learn a lot, thanks!

Post Feedback

Title:
Name:
Url:
Comments: 
Codice di sicurezza
Protected by FormShield