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

  • SEARCH
  • Home
  • 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 4002168
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:59:35+00:00 2026-05-20T07:59:35+00:00

I have a generic base repository class for LINQ-to-Entities that can do things like

  • 0

I have a generic base repository class for LINQ-to-Entities that can do things like selecting an entity based on the ID.

In order to subclass it, you need to implement these two:

protected override Expression<Func<User, bool>> GetByIDSelector(int id)
{
  return u => u.UserID == id;
}

protected override Expression<Func<User, int>> KeySelector
{
  get { return u => u.UserID; }
}

I would like to just have the KeySelector (which is used for default sort order).

My ‘get by ID’ method looks like this:

public virtual TDataModel GetByID(TKey id)
{
  var entity = SetProvider.Set<TDataModel>().Where(GetByIdSelector(id)).SingleOrDefault();

  if (entity == null)
  {
    throw new EntityNotFoundException<TDataModel>(id);
  }

  return entity;
}

Where GetByIdSelector looks like this, which should turn the KeySelector expression into one that can select by ID.

private Expression<Func<TDataModel, bool>> GetByIdSelector(TKey id)
{
  return Expression.Lambda<Func<TDataModel, bool>>
  (
    Expression.Equal(KeySelector.Body,
    Expression.Constant(id)), 
    KeySelector.Parameters.Single()
  );
}

However, the Entity Framework throws an exception that the parameter passed in by that isn’t bound to the query.

The parameter ‘u’ was not bound in the specified LINQ to Entities query expression.

Is there any way I can cleanly reuse the KeySelector Expression without having to rebuild the entire thing?

  • 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-05-20T07:59:36+00:00Added an answer on May 20, 2026 at 7:59 am

    The reason this doesn’t work is because every time you call this.KeySelector, it’s creating a brand new lambda expression (hence the call to KeySelector.Parameters.Single() is not the same parameter as the parameter used for KeySelector.Body). Cache that expression into a variable as follows and it will work.

    private Expression<Func<TDataModel, bool>> GetByIdSelector(TKey id)
    {
        var keySelector = KeySelector;
        return Expression.Lambda<Func<TDataModel, bool>>(
            Expression.Equal(
                keySelector.Body,
                Expression.Constant(id)
            ), 
            keySelector.Parameters.Single()
        );
    }
    

    What I would probably do is rather fix the code in the KeySelector property to use a fixed value such that the value retrieved is always the same instance.

    Example:

    private static readonly Expression<Func<User, int>> _keySelector = u => u.UserID;
    
    protected override Expression<Func<User, int>> KeySelector
    {
        get { return _keySelector; }
    }
    

    Then if you do that – your old code for GetByIdSelector is actually no worse than my version above.

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

Sidebar

Related Questions

Imagine that I have a generic base class like this: public abstract class AnimalDTO<TA,
I have a repository pattern setup using NHibernate. The base class looks like this:
If I have a generic class like this: public class Repository<T> { public string
So basically i have a domain object and a generic repository that can do
I have a generic abstract base class from which I would like to derive
I have a base class which is non-generic with a derived generic class. The
I have a generic class that should allow any type, primitive or otherwise. The
I have a generic Repository<T> class I want to use with an ObjectDataSource. Repository<T>
I have a generic class that I'm trying to implement implicit type casting for.
I have a generic class in C# with 2 constructors: public Houses(params T[] InitialiseElements)

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.