Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8959561
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:28:15+00:00 2026-06-15T15:28:15+00:00

I have read about 30 Stack Overflow questions, and about 20 blogs and cannot

  • 0

I have read about 30 Stack Overflow questions, and about 20 blogs and cannot find my answer. That said, I am sure the answer I require is out there so if you know of it please point it out (please note the last paragraph/sentence about answers that do not fit my requirement). Thanks!

I am persisting instances of classes that have a similar shape to:

public class Data {
    public int Id { get; set; }
}

public class Container {
    public int Id { get; set; }
    public Data Data { get; set; }
}

Currently queries are written to find Containers via an abstraction layer that requires a Lambda Expression accepting a Container and returning bool (predicate). It will not accept IQueryable even though Entity Framework 5 is the ORM of choice.

My task is to present an API surface based around Lambda Expressions that accept the Data type. I cannot change the abstraction layer (I must pass a predicate that accepts a Container) so I am trying to convert an expression I receive as:

Expression<Func<Data , bool>>
to:
Expression<Func<Container , bool>>

I have added an extra method in my repository class like these:

public Container Find( Expression<Func<Data , bool>> predicate ) {

    IEnumerable<Container> result = QueryStrategy.Fetch( c => predicate.Compile().Invoke( c.Data ) );

    return result.FirstOrDefault();

}

This compliments the existing Find method:

public Container Find( Expression<Func<Container , bool>> predicate ) {

    IEnumerable<Container> result = QueryStrategy.Fetch( predicate );

    return result.FirstOrDefault();

}

When the former method is used it produces the following exception:

LINQ to Entities does not recognize the method ‘Boolean
Invoke(Container.Data)’ method, and this method cannot be
translated into a store expression.

I have tried all sorts of things with Expression classes I just can’t see a way to map:

Expression<Func<Data , bool>>
to:
Expression<Func<Container , bool>>

Without using Invoke which is not supported by Entity Framework (but works fine with in memory enumerable data).

Can anybody help me get the above scenario working using Expressions?

I understand that I may be able to use the LinqKit library to solve this, but I really want to solve this problem without bringing in a third party library.

UPDATE: In my effort to present a simplified problem, I have implied (by originally using DbContext in the code sample) that the code would have access to IQueryable and/or be suitable for using LinqKit’s AsExpandable(). In reality this is not the case – the repository class is not allowed to use IQueryable or any vendor specific extensions. I have modified the examples above and hopefully this makes things clearer.

This Issue Is Now Solved

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:28:17+00:00Added an answer on June 15, 2026 at 3:28 pm

    I have managed to solve the problem after 4 hours of reading/tinkering with Expressions (amazing technology).

    I have put this class together (it is not polished or final but shows how it was achieved):

    class ParameterRewriter<TTarget , TSource> : ExpressionVisitor {
    
        private ParameterExpression Source;
        private MemberExpression Target;
    
        public Expression<Func<TTarget , bool>> Rewrite( Expression<Func<TSource , bool>> predicate , Expression<Func<TTarget , TSource>> propertyNameExpression ) {
    
            var parameter = Expression.Parameter( typeof( TTarget ) );
    
            var propertyName = ( propertyNameExpression.Body as MemberExpression ).Member.Name;
    
            Source = predicate.Parameters.Single();
            Target = Expression.PropertyOrField( parameter , propertyName );
    
            var body = Visit( predicate.Body );
    
            return Expression.Lambda<Func<TTarget , bool>>(
                body ,
                parameter
            );
    
        }
    
        protected override Expression VisitParameter( ParameterExpression node ) {
    
            if ( node == Source ) {
                return Target;
            }
    
            return base.VisitParameter( node );
    
        }
    
    }
    

    And it can be used like this:

    var parameterRewriter = new ParameterRewriter<Container , Data>();
    Expression<Func<Data , bool>> dataPredicate = d => ( d.Id == 1 );
    var containerPredicate = parameterRewriter.Rewrite( dataPredicate , c => c.Data );
    

    In theory one should be able to traverse deeper relationships by examining propertyNameExpression but I have had enough for today.

    I can now see that the SQL Entity Framework has generated for each flavour of query is identical:

    ========================

    Expression<Func<Container , bool>> p = c => c.Data.Id == 1
    
    SELECT
    [Extent1].[Id] AS [Id],
    [Extent1].[Data_Id] AS [Data_Id]
    FROM [dbo].[Container] AS [Extent1]
    WHERE 1 = [Extent1].[Data_Id]
    

    ========================

    Expression<Func<Data , bool>> p = d => d.Id == 1
    
    SELECT
    [Extent1].[Id] AS [Id],
    [Extent1].[Data_Id] AS [Data_Id]
    FROM [dbo].[Container] AS [Extent1]
    WHERE 1 = [Extent1].[Data_Id]
    

    ========================

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have read this article from High Scalability about Stack Overflow and other large
I have read countless blogs, posts and StackOverflow questions about the new features of
Recently, I read a post on Stack Overflow about finding integers that are perfect
I've read a large number of articles and Stack Overflow questions about converting an
I read some similar questions about this on Stack Overflow, but none of them
I've read all the posts on Stack Overflow about CreateProcessAsUser and there are very
I have found a few similar questions here in Stack Overflow, but I believe
I've read several of the posts on stack overflow already about this topic, but
I have read some questions about this in the last week or so, on
I have read more than 3 questions here in stackoverflow about this, but all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.