26

I'm using LinqKit library which allows combining expressions on the fly.

This is a pure bliss for writing Entity Framewok data acess layer because several expressions can optionally be reused and combined, which allows both for readable and efficient code.

Consider following piece of code:

private static readonly Expression<Func<Message, int, MessageView>> _selectMessageViewExpr =
    ( Message msg, int requestingUserId ) =>
        new MessageView
        {
            MessageID = msg.ID,
            RequestingUserID = requestingUserId,
            Body = ( msg.RootMessage == null ) ? msg.Body : msg.RootMessage.Body,
            Title = ( ( msg.RootMessage == null ) ? msg.Title : msg.RootMessage.Title ) ?? string.Empty
        };

We declare an expression that projects Message onto MessageView (I removed the details for clarity).

Now, the data access code can use this expression to get individual message:

var query = CompiledQueryCache.Instance.GetCompiledQuery(
    "GetMessageView",
    () => CompiledQuery.Compile(
        _getMessagesExpr
            .Select( msg => _selectMessageViewExpr.Invoke( msg, userId ) ) // re-use the expression
            .FirstOrDefault( ( MessageView mv, int id ) => mv.MessageID == id )
            .Expand()
        )
    );

This is beautiful because the very same expression can be reused for getting a message list as well:

var query = CompiledQueryCache.Instance.GetCompiledQuery(
    "GetMessageViewList",
    () => CompiledQuery.Compile(
        BuildFolderExpr( folder )
            .Select( msg => _selectMessageViewExpr.Invoke( msg, userId ) )
            .OrderBy( mv => mv.DateCreated, SortDirection.Descending )
            .Paging()
            .Expand()
        ),
    folder
    );

As you can see, projection expression is stored in _selectMessageViewExpr and is used for building several different queries.

However, I spent a lot of time tracing a strange error where this code crashed at Expand() call.
The error said:

Unable to cast object of type System.Linq.Expressions.FieldExpression to type System.Linq.Expressions.LambdaExpression.

It's only after a while that I realized that everything works when expression is referenced in a local variable before being called Invoke on:

var selector = _selectMessageViewExpr; // reference the field

var query = CompiledQueryCache.Instance.GetCompiledQuery(
    "GetMessageView",
    () => CompiledQuery.Compile(
        _getMessagesExpr
            .Select( msg => selector.Invoke( msg, userId ) ) // use the variable
            .FirstOrDefault( ( MessageView mv, int id ) => mv.MessageID == id )
            .Expand()
        )
    );

This code works as expected.

My question is:

Is there any specific reason why LinqKit doesn't recognize Invoke on expressions stored in fields? Is it just an omission by developer, or is there some important reason why expressions need to be stored in local variables first?

This question can probably be answered by looking at generated code and checking LinqKit sources, however I thought maybe someone related to LinqKit development could answer this question.

Thanks.

Dan Abramov
  • 264,556
  • 84
  • 409
  • 511
  • Did you find an answer to this? – Lawrence Wagerfield Jul 27 '11 at 08:36
  • @Lawrence: no, I'm still curious. Currently I use the workaround described. – Dan Abramov Jul 27 '11 at 08:43
  • I just ran into this in my project. As far as I can tell from looking at the LinqKit source it seems to be because the ExpressionExpander is not programmed to handle property fields. It does not know how to call 'get' since its working based on reflection. I am trying to figure out a solution so ill bookmark this – Charles Aug 03 '11 at 21:18
  • @Charles: cool. I didn't actually expect anyone to answer or comment on this question so it's quite some fun to see the responses. – Dan Abramov Aug 03 '11 at 21:20
  • 1
    +1, I didn't realize why I was having the error until I saw your question, which made me realize I needed to store it in a local variable first. Thanks! – DCShannon May 01 '15 at 03:35

2 Answers2

25

I downloaded sourcecode and tried to analyse it. ExpressionExpander does not allow to reference expressions that are stored in variables other than constant. It expects expression that Invoke method is being called upon to reference to object represented by ConstantExpression, not another MemberExpression.

So we cannot provide our reusable expression as reference to any member of the class (even public fields, not properties). Nesting member access (like object.member1.member2 ... etc) is not supported too.

But this can be fixed by traversing initial expression and recusrsively extracting subfields values.

I have replaced TransformExpr method code of ExpressionExpander class to

var lambda = Expression.Lambda(input);
object value = lambda.Compile().DynamicInvoke();

if (value is Expression)
    return Visit((Expression)value);
else
    return input;

and it works now.

In this solution everything I mentioned before (recursively traversing tree) is done for us by ExpressionTree compiler :)

Mic
  • 810
  • 10
  • 15
  • One hell of a answer. I didn't expect this to be answered at all, honestly. Thank you. – Dan Abramov Oct 18 '11 at 19:57
  • As a side note, you don't really need JS for formatting—just make sure you familiarize yourself with Markdown. You can see my edit, for start. Thanks again! – Dan Abramov Oct 18 '11 at 19:58
  • Thanks:) I must say i didnt check all cases and expression trees so I cannot guarantee it will work fine alaways. Maybe further tweaking is a must. LinqKit code is rather complicated:) – Mic Oct 19 '11 at 20:18
  • I tried out your code, and while it did get me past LinqKit's error, ELinq is now giving me the following error. Any ideas? Here's the error: "Unable to create a null constant value of type '...'. Only entity types, enumeration types or primitive types are supported in this context." – Josh Mouch May 12 '12 at 02:49
  • Nevermind. I don't think this error has anything to do with your code. I have a "(x == null) ? null : ...." at the start of my expression, and if I remove that, it works. – Josh Mouch May 12 '12 at 03:02
  • 1
    Mic after using your solution I hit another exception: `variable 'p' of type '(...)' referenced from scope '', but it is not defined`. Solution was skipping some input: `if (input == null || (input.NodeType == ExpressionType.MemberAccess && !(input.Member is FieldInfo))) return input;` – Yankes Apr 23 '13 at 21:32
10

I created improved version of Mic answer:

if (input == null)
    return input;

var field = input.Member as FieldInfo;
var prope = input.Member as PropertyInfo;
if ((field != null && field.FieldType.IsSubclassOf(typeof(Expression))) ||
    (prope != null && prope.PropertyType.IsSubclassOf(typeof(Expression))))
    return Visit(Expression.Lambda<Func<Expression>>(input).Compile()());

return input;

Main advantage is removal DynamicInvoke that have big overhead and calling Invoke only when I really need it.

Community
  • 1
  • 1
Yankes
  • 1,958
  • 19
  • 20