Using .AsExpandable() from LinqKit with Breeze. (Refactoring out .AsExpandable())

Posted on Sep 9, 2015 | Categories: coding, linq

This is a little story about the IQueryable which had to be queried.

While I was working on a solution which featured Breeze to handle the API on the server side (.NET and EF) I ran into an issue with the .AsExpandable() function from LinqKit. While this function works great with regular code Breeze can start complaining when you start using the expand functions from the client side. You will get error messages like ‘Cannot compare elements of type…‘ or ‘Only primitive types, enumeration types and entity types are supported.’. If you don’t know about the .AsExpandable() function yet I strongly advice you to visit for more information about LinqKit and this specific function.

These errors are caused because the System.Linq.IQueryable.Provider on the IQueryable is of the type LinqKit.ExpandableQueryProvider<T> instead of System.Data.Entity.Internal.Linq.DbQueryProvider.

The code I had looked like this:

int[] ids = { 1, 2, 3, 4 };
var values = queryable
               .Where(q => ids.Contains(

The goal of the code above is to take items from the queryable where a specific identifier’s result (of the type Expression<Func<T, int>>) had to be in the ids array.

Based on the documentation ( this can also be written like this:

int[] ids = { 1, 2, 3, 4 };
Expression<Func<T, bool>> identifier = q => ids.Contains(identifier.Invoke(q));
var values = queryable.Where(identifier.Expand());

Which in turn solves the problem with the IQueryable Provider.