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 8743103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:37:03+00:00 2026-06-13T11:37:03+00:00

I am writing a generic repository to interface with EF using DBContext. I have

  • 0

I am writing a generic repository to interface with EF using DBContext.

I have a generic Get() method which receives a primary key value and returns the entity:

public class DALRepository<DALEntity> : IDisposable, IGenericRepository<DALEntity> where DALEntity : class
{
private IDbSet<DALEntity> dbSet;
private NWEntities context;

public DALRepository()
{
  context = new NWEntities();
  context.Configuration.LazyLoadingEnabled = false;
  dbSet = context.Set<DALEntity>();
}

Here’s a simple get method – just works on the PK – exactly what I want.

public DALEntity Get(string ID)
{
   return dbSet.Find(ID);
}

I now want to change this to allow the consumer to pass in a list of includes – so as well as returning just a customer they can request to return the orders as well. Here’s where I’m running into trouble. If I do this:

public DALEntity Get(string ID, IEnumerable<string> IncludeEntities = null)
{
      IQueryable<DALEntity> query = dbSet;
      query = IncludeEntities.Aggregate(query, (current, includePath) => current.Include(includePath));
}

I can’t use find with the IQueryable. And I can’t Find() directly because I can’t pass includes to it. If I use a where lambda instead on the IQueryable, how do I tell it to use the PK of the entity? I guess I could have a generic constraint that insists the generic type must implement some IPkey interface with a well-defined primary column name such as “ID”, but then I can’t use the entities generated by DBContext as they woudl have to implement this interface. I can change the T4 to do this if I need – and I’ve already changed it to emit XML comments so not too averse to that – but does anyone have a simpler way? I suppose what I need is an overloaded find() which accepts a list of includes.

So my question is either how to use Find with includes, or how to write the lambda where it knows the PK? I can’t receive such a lambda as a parameter as this will ultimately be consumed by a WCF service.

  • 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-13T11:37:03+00:00Added an answer on June 13, 2026 at 11:37 am

    Kind of wierd answering your own question but in case anyone else has this issue here’s what I did. I used the dynamic LINQ stuff and used the string overloaded version of .Where(). I used one of the links mentioned to figure out how to grab the primary key (and mine is from a DBContext as well), and the method now looks like this:

    public DALEntity Get(string ID, IEnumerable<string> IncludeEntities = null)
    {
      var set = ((IObjectContextAdapter)context).ObjectContext.CreateObjectSet<DALEntity>();
      var entitySet = set.EntitySet;
      string[] keyNames = entitySet.ElementType.KeyMembers.Select(k => k.Name).ToArray();
      Debug.Assert(keyNames.Length == 1, "DAL does not work with composite primary keys or tables without primary keys");
    
      IQueryable<DALEntity> query = dbSet;
      query = IncludeEntities.Aggregate(query, (current, includePath) => current.Include(includePath));
    
      query = query.Where(keyNames[0] + "= @0", ID);
      return query.FirstOrDefault();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a generic Future<T> class which encapsulates getting a value asynchronously and I've
I was just writing a generic object factory and using the boost preprocessor meta-library
I'm having a problem writing a generic method to retrieve AD Groups or Users
I am writing following code, using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace
I am writing a generic repository for entity framework and am confused as to
I'm writing generic logger for SQLException and I'd like to get parameters that were
When writing a generic method to process data for a form, I came across
I'm writing a generic wrapper class for a bunch of classes we have defined
I am writing a generic repository to be used for my every model CRUD
I have an asynchronous socket listener class like this: using System; using System.Collections.Generic; using

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.