LINQ to Entities: Client-side Expressions in Final Projection

Topics: EF Runtime
Dec 13, 2012 at 10:31 AM
Edited Dec 14, 2012 at 9:22 AM

Dear Friends,

There's a potential feature that is missing in LINQ to Entities that I would like to discuss here i.e. being able to resolve and materialize queries with client-side expressions in their final projection (select) part. For example a query like:

var query = from a in myModel.Foo
            select new {
                Output = string.Format(a.FormatString, a.Parameter1, a.Parameter2, a.Parameter3),
            };

Currently causes LINQ to Entities provider to throw an exception indicating that string.Format is not a recognized method for LINQ to Entities. What I expect from LINQ to Entities is fetching the inputs for evaluating the client-side expressions like string.Format and evaluate these expressions during the materialization process.

I understand that queries like what I've presented cannot be further composed with LINQ operators anymore and allowing the programmer to write such queries might require a considerable insight/awareness from the programmer to distinguish which parts of his/her query is evaluated server-side (i.e. SQL Server or other DBMS) and which part is evaluated client-side (the application executing the query) but I still find it useful preventing use of more complex and unreadable notions such as:

 var query = (from a in myModel.Foo
             select new { 
                 a.FormatString, 
                 a.Parameter1, 
                 a.Parameter2 
            })
            .ToList()
            .Select(a => new {
                Output = string.Format(a.FormatString, a.Parameter1, a.Parameter2, a.Parameter3),
            });

BTW, the very same feature is available in LINQ to SQL and as an experienced user of LINQ to SQL I have wrote a dozen of apps in production that are heavily utilizing it. And Lack of this feature is the only thing that makes it hard for me to migrate my existing code to EF.

I would like to know the opinion of EF team/community about this feature and if you also find it useful, I would like to implement and submit it as a contribution to EF base.

Developer
Dec 19, 2012 at 5:52 PM

Hi arashr,

Thanks for starting this discussion. This is certainly something we have thought about in the past. Some key members of the team are on vacation right now, but we will discuss this in January when they return and we will get back to you.

Thanks,
Arthur

Developer
Jan 11, 2013 at 10:59 PM

Hi arashr,

We talked about this today and it is something that we are interested in. The team did some initial thinking about how it could be implemented in our stack a couple of years ago and Diego is going to try to dig up that information and post it on the CodePlex site. That should act as the starting point for how to implement this if you choose to make the contribution.

Thanks,
Arthur

Jan 12, 2013 at 6:50 AM

Hi Arthur,

 
Thank you for you answer! I am happy to hear that. During the past weeks, I have investigated the source code of EF LINQ provider more thoroughly and also came with some ideas on how to implement this feature. I will of course follow the information provided on CodePlex site and try to keep myself on track with EF team's original idea.
 
Best regards,
Arash
Feb 28, 2013 at 11:47 AM
Hi Arthur,

It been a long time since our discussion and during this time, I have been checking the site regularly waiting for the design/implementation notes to appear on the website but couldn't ever find it. And in my spare time, I came up with a working but not-so-clean implementation of my own but I decided to wait and see the notes in order to contribute a clean and production-quality feature. Now, I am wondering whether it is going to be some update about this feature from the team or not.

I would be glad if you could update me about it.

Thanks,
Arash
Coordinator
Mar 1, 2013 at 9:43 PM
Hello Arash,

Arthur pointed me to this thread some time ago. Sorry if it has taken so long for me to follow up. As Arthur mentioned, we are very interested in removing some of the constraints that exist for client-side evaluation in LINQ to Entities and we would love to see a contribution in this area.

I am actively working on preparing a document that details various specific changes we have identified as well as some challenges we need to overcome. I expect to have this document uploaded next week.

In the meanwhile it would be interesting to discuss more in detail the scenarios you would like to enable. If you wanted to send a pull request just to get feedback on the design, that would work too. Just make sure you follow the instruction for contributing to EF.

Thanks,
Diego
Developer
Mar 6, 2013 at 4:52 PM