1

I have Parent and Child entities in database. I need to get some of Childs with their parents as ChildBrief and ParentBrief objects respectively that contain only some of fields of the corresponding entities. Thus, ParentBrief and ChildBrief are kind of maps of underlying entities.
I can write:

IQueriable<ChildBrief> result = Childs.Select(x => new ChildBrief
    {
        Id = x.Id,
        Name = x.Name,
        Parent = new ParentBrief
        { 
            Id = x.Parent.Id, 
            Name = x.Parent.Name
        }
    });

and it will be perfectly compiled into resulting SQL. But i want the ParentBrief mapper to be a separate static function that can be re-used in other places like here and i do the following:

IQueriable<ChildBrief> result = Childs.Select(x => new ChildBrief
    {
        Id = x.Id,
        Name = x.Name,
        Parent = mapper.Compile()(x.Parent)
    });

private static readonly Expression<Func<Parent, ParentBrief>> mapper =
    m => new ParentBrief
    {
        Id = x.Parent.Id, 
        Name = x.Parent.Name}
    };

EF5 raises an error saying unable to invoke method or something like this (probably at the Compile stage).
Are there any ways to inject simple expressions (just flat mapping) into EF and make them compiled correctly to the SQL? I also want to get IQueryable results with no upfront execution.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
LINQ2Vodka
  • 2,996
  • 2
  • 27
  • 47
  • 1
    This is one way to "inject" an IQueryable. You create your own IQueryable extension method and tag it with `[Expandable]` so that the engine can replace your reusable method with the IQueryable returned: http://stackoverflow.com/questions/10826275/iqueryable-extension-method-for-linq2entities – AaronLS Feb 14 '13 at 23:06
  • 1
    I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 14 '13 at 23:07
  • @JohnSaunders Be careful, that is a guidline and shouldn't be followed as a hard rule. If I tag this question with expression and injection tags then the title should be empty? – AaronLS Feb 14 '13 at 23:09
  • @AaronLS thank you. Looks really tricky... – LINQ2Vodka Feb 14 '13 at 23:22
  • Can you show me how "EF" was being used as something other than a tag? That is, was it not being added to your title for the sole purpose of categorizing your question? Your title could certainly have been better in any case. I would suggest you indicate what you're injecting LINQ expressions _into_, for instance. – John Saunders Feb 14 '13 at 23:32
  • @jim indeed the implementation is very tricky and you might find scenarios that it doesn't work well. It's the only way I've found to reuse linq expressions/logic, but not really polished enough to work well in all scenarios. If I wasn't tired and unmotivated all the time I'd work on making it into a project. – AaronLS Feb 14 '13 at 23:35
  • John, the problem looks really bound to the EF expression evaluation implementation that's why i added it. – LINQ2Vodka Feb 14 '13 at 23:37
  • 1
    @AaronLS i hope MS will implement it one day :) – LINQ2Vodka Feb 14 '13 at 23:38
  • @JohnSaunders This question is specific to EF, as EF doesn't support custom IQueryable extentions(even if they return a compliant IQueryable) since it doesn't evaluate your extension. So I immediately knew what the asker**(who BTW is not me)** was asking about. All of that is besides the point. Point is you should be cautious about using this hammer and wacking at titles with a generalized rule. I'm just recommending you do so cautiously, and the reason you should exercise caution is exemplified by the example I gave you. You could easily reduce a title down to something that is ambiguous. – AaronLS Feb 14 '13 at 23:43
  • Sorry, I've been doing this a while, and my rationale is simple. "EF:" was not properly part of the title. It characterized the question as being specific to EF (as you've said). The correct way to characterize a question as being specific to EF is to add the [tag:entity-framework] tag, not to add "EF:" to the title. If you have a question about that, then please start a discussion on [meta] and post a link to it here (so I'll see it and join the discussion). – John Saunders Feb 14 '13 at 23:46

0 Answers0