Client-side properties and any remote LINQ provider

June 24th 2009 • .NET (, , ) • 3,213 views • 13 responses

David Fowler on the ASP.NET team and I have been bouncing ideas about on how to solve an annoyance using LINQ:

If you write properties on the client you can’t use them in remote LINQ operations.

The problem occurs because these properties can’t be translated and sent to the server as they have been compiled into intermediate language (IL) and not LINQ expression trees that are required for translation by IQueryable implementations. There is nothing available in .NET to let us reverse-engineer the IL back into the methods and syntax that would allow us to translate the intended operation into a remote query.

This means you end up having to write your query in two parts; firstly the part the server can do, a ToList or AsEnumerable call to force that to happen and bring the intermediate results down to the client, and then the operations that can only be evaluated locally. This can hurt performance if you want to reduce or transform the result set significantly.

What we came up (David, Colin Meek and myself) is a provider-independent way of declaring properties just once so they can be used in both scenarios. Computed properties for LINQ to SQL, LINQ to Entities and anything else LINQ enabled with little effort and it works great on .NET 3.5 SP1 :)

Before example

Here we have extended the Employee class to add Age and FullName. We only wanted to people with “da” in their name but we are forced to pull down everything to the client in order to the do the selection.

partial class Employee {
	public string FullName {
		get { return Forename + " " + Surname; }
	}

	public int Age {
		get { return DateTime.Now.Year - BirthDate.Year -
			(DateTime.Now.Month < BirthDate.Now.Month
			|| DateTime.Now.Month == BirthDate.Now.Month && DateTime.Now.Day < BirthDate.Now.Day) ? 1 : 0);
		}
	}
}
...
var employees = db.Employees.ToList().Where(e => e.FullName.Contains("da")).GroupBy(e => e.Age);

After example

Here using our approach it all happens server side… and works on both LINQ to Entities and LINQ to SQL.

partial class Employee {
    private static readonly CompiledExpression<Employee,string> fullNameExpression
     = DefaultTranslationOf<Employee>.Property(e => e.FullName).Is(e => e.Forename + " " + e.Surname);
    private static readonly CompiledExpression<Employee,int> ageExpression
     = DefaultTranslationOf<Employee>.Property(e => e.Age).Is(e => DateTime.Now.Year - e.BirthDate.Value.Year - ((DateTime.Now.Month < e.BirthDate.Value.Month || (DateTime.Now.Month == e.BirthDate.Value.Month && DateTime.Now.Day < e.BirthDate.Value.Day)) ? 1 : 0)));

    public string FullName {
        get { return fullNameExpression.Evaluate(this); }
    }

    public int Age {
        get { return ageExpression.Evaluate(this); }
    }
}
...
var employees = db.Employees.Where(e => e.FullName.Contains("da")).GroupBy(e => e.Age).WithTranslations();

Getting started

Check out this download which includes the necessary project to drop-in to your solution. The caveat to the usage technique shown above is you need to ensure your class has been initialized before you write queries to it. If this is a problem check out the usage considerations section below.

The other major caveat is obviously the expression you register for a property must be able to be translated to the remote store so you will need to constrain yourself to the methods and operators your IQueryable provider supports.

Usage considerations

There are a few alternative ways to use this rather than the specific examples above.

Registering the expressions

You can register the properties in the class itself as shown in the examples which means the properties themselves can evaluate the expressions without any reflection calls. Alternatively if performance is less critical you can register them elsewhere and have the methods look up their values dynamically via reflection. e.g.

...
DefaultTranslationOf<Employee>.Property(e => e.FullName).Is(e => e.Forename + " " + e.Surname);
var employees = db.Employees.Where(e => e.FullName.Contains("da")).GroupBy(e => e.Age).WithTranslations();
...
partial class Employee {
    public string FullName { get { return DefaultTranslationOf<Employees>.Evaluate<string>(this, MethodInfo.GetCurrentMethod());} }
}

If performance of the client-side properties is critical then you can always have them as regular get properties with the full code in there at the expense of having the calculation duplicated, once in IL in the property and once as an expression for the translation.

Different maps for different scenarios

Sometimes certain parts of your application may want to run with different translations for different scenarios, performance etc. No problem!

The WithTranslations method normally operates against the default translation map (accessed with DefaultTranslationOf) but there is also another overload that takes a TranslationMap you can build for specific scenarios, e.g.

var myTranslationMap = new TranslationMap();
myTranslationMap.Add<Employees, string>(e => e.Name, e => e.FirstName + " " + e.LastName);
var results = (from e in db.Employees where e.Name.Contains("martin") select e).WithTranslations(myTranslationMap).ToList();

Not specifying WithTranslation everywhere

If you are happy to always have the default translation applied simply add the following statement to the top of your file which will bring in a bunch of extensions methods for the usual LINQ query operators that already apply WithTranslation for you :)

using Microsoft.Linq.Translations.Auto;

With .NET 4.0

You should be able to drop the ExpressionVisitor class in the download and reference the built-in one.

How it works

CompiledExpression<T, TResult>

The first thing we needed to do was get the user-written client-side “computed” properties out of IL and back into expression trees so we could translate them. Given that we also want to evaluate them on the client we need to compile them at run time so CompiledExpression exists which just takes an expression of Func<T, TResult>, compiles it and allows evaluation of objects against the compiled version.

ExpressiveExtensions

This little class provides both the WithTranslations extensions methods and the internal TranslatingVisitor that unravels the property accesses into their actual registered Func<T, TResult> expressions via the TranslationMap so that the underlying LINQ provider can deal with that instead.

TranslationMap

We need to have a map of properties to compiled expressions and for that purpose TranslationMap exists. You can create a TranslationMap by hand and pass it in to WithTranslations if you want to programmatically create them at runtime or have different ones for different scenarios but generally you will want to use…

DefaultTranslationOf

This helper class lets you register properties against the default TranslationMap we use when nothing is passed to WithTranslations. It also allows you to lookup what is already registered so you can evaluate to that although there is a small reflection performance penalty for that:

public int Age { get { return DefaultTranslationOf<Employees>.Evaluate<int>(this, MethodInfo.GetCurrentMethod()); } }

AutoTranslation

Remembering to specify WithTranslations everywhere can be a pain so if you always want to go via translation and use the default TranslationMap you can include the Microsoft.Linq.Translations.Auto namespace in your code which includes extension methods for all the regular LINQ operations that saves you having to remember.

Have fun!

[)amien

Related content

13 responses  

  1. KristoferA on June 24th, 2009

    This is *very* neat, thanks for the sample & the translation lib. This is at least as handy as System.Linq.Dynamic (which is one of my favorites :) ).

    Just one note though: when used as the only discriminator in a where clause like in the full name example, SQL Server will be unable to use any indexes on the underlying fields. In other words, this is fine to use in projections, as ‘non-primary’ discriminators, or on small tables/result sets.

    E.g. the fullname where clause will translate to something along the lines:
    WHERE ((([t0].[Forename] + @p0) + [t0].[Surname]) LIKE @p1)
    …and that basically tells SQL Server to concatenate forename + p0 + surname in all records in the table and then apply the like comparison…

  2. Pingback Dew Drop – June 25, 2009 | Alvin Ashcraft's Morning Dew on June 25th, 2009

    [...] Client-side properties and any remote LINQ provider (Damien Guard) [...]

  3. Pingback I love .NET! » Blog Archive » The Technology Post for June 25th, 2009 on June 25th, 2009

    [...] ORM – Client-side properties and any remote LINQ provider – Damien Guard (Suggested by David Fowler) [...]

  4. shawn on June 25th, 2009

    Nice, now that’s what I’m talking about! Any chance we can get something like this baked into the framework for 4.0? ;)

  5. Pingback The Technology Post for June 25th, 2009 | Nexo IT - Information Technology News on June 25th, 2009

    [...] ORM – Client-side properties and any remote LINQ provider – Damien Guard (Suggested by David Fowler) [...]

  6. BrianP on June 28th, 2009

    This is great! I can’t wait to try it out.

    I agree with shawn, this should be standard part of linq.

  7. BrianP on June 28th, 2009

    “The caveat to the usage technique shown above is you need to ensure your class has been initialized before you write queries to it. If this is a problem check out the usage considerations section below.”

    I would like to keep the property expressions in the class itself, are there any other recommended ways of ensuring the class gets initialized before querying?

  8. Pingback Summary 13.07.2009 « Bogdan Brinzarea’s blog on July 13th, 2009

    [...] Guard shows how to use client-side properties in LINQ remote queries eliminating the need to retrieve all the data from the server side and applying additional [...]

  9. Damien Guard on August 11th, 2009

    @BrianP, something simple like typeof(MyClass).ToString(); should cause it to be initialized.

    [)amien

  10. Shaul B on March 23rd, 2010

    Very cool, thank you!
    Just one problem – if you include “using Microsoft.Linq.Translations.Auto;” it creates an ambiguity with System.Linq.Queryable.Where extension, as well as the System.Linq.Queryable.Contains extension. How do you work around that?

  11. David Fowler on March 24th, 2010

    @Shaul B

    Do you have both of the namespaces included? (i.e the default Linq namespace and Microsoft.Linq.Translations.Auto) They can’t be used together since one overrides the other’s behavior and have the same signatures.

  12. Shaul B on April 7th, 2010

    @David Fowler:
    Yes, I was trying to include both namespaces. I can’t omit System.Linq, because that has some other extensions (e.g. First()) that I also need to use.
    Effectively this means that I can’t use the Auto translations at all, which is a big pity… :(

  13. Matyas Boros on August 30th, 2010

    @David: You can create the missing extension methods easily by hand, and then not use the original System.Linq ones. You have the examples in the AutoTranslation.cs file. There is no magic, each one is 1 line. I agree that in the next update these should be included by default,

    My problem is a bit different as the typeof(MyClass).ToString() does not initialize the static fields.
    Checking the c# language specification for 3.0 (point 10.5.5.1 Static field initialization), the static field is initialized before it’s access (what can be too late in our case if there is an sql query executed with this property usage), or before the static constructor. The static constructor is called when a static field is accessed, or an instance is created. So for this to work properly, we have to make sure, that all Entities having such properties have a static constructor (it can be empty), and that a static field is called or an instance is created.
    So find a suitable place that gets called only once, and before any query gets executed (either Global.Application_Start(..) or a surely accessed class’s static constructor – in my case my UserControlBase class), and either call a customly defined static field on the entity, or create an instance of it.
    It is quite an ugly workaround, and i would be very interested in any better ones!! Ideas?

Leave your response

  1. (kept private)