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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:58:43+00:00 2026-06-08T21:58:43+00:00

I currently have a complete generic repository but I’m missing one feature and that

  • 0

I currently have a complete generic repository but I’m missing one feature and that is to use
Include() and Find() together.

So now I have:

public E FindById<E>(int id) where E : class
{
    return DataContext.Set<E>().Find(id);
}

called using

var person = PersonRepo.FindById<Person>(personId);

I would like to have something similar to:

var person = PersonRepo.FindByIdWithIncludes<Person>(personId,new[]{"State.Address"});

So, something along this lines (this is only a test):

public E FindByIdWithIncludes<E>(int id, string[] includes) where E : class
{
    var entitySet = DataContext.Set<E>();
    DbQuery<E> entityQuery;

    foreach (var include in includes)
    {
        entityQuery = entitySet.Include(include);
    }

    return entityQuery.Find(id); //this is were it breaks
}

Is it possible?

  • 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-08T21:58:46+00:00Added an answer on June 8, 2026 at 9:58 pm

    You cannot use Find directly – Find doesn’t work with includes. You must use SingleOrDefault.

    First you need to define interface for your entities to expose their key.

    public interface IEntityWithId 
    {
        public int Id { get; set; }
    }
    

    Next you can write simple method with constrain to get access to the key:

    public E FindByIdWithIncludes<E>(int id, string[] includes) 
        where E : class, IEntityWithId
    {          
    
        IQueryable<E> entityQuery = DataContext.Set<E>();
    
        foreach (var include in includes)
        {
                entityQuery = entityQuery.Include(include);
        }
    
        return entityQuery.SingleOrDefault(e => e.Id == id); 
    }
    

    Btw. you can use strongly typed includes – here is an example.

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

Sidebar

Related Questions

I currently have one project that currently contains multiple packages. These packages make up
I currently have a single solution that contains both the one application developed so
I currently have a complete cover background image. Here is the code: #back{ /*
Currently have a drop down menu that is activated on a hover (from display:none
I currently have a XSLT 2.0 Stylesheet that I am trying to remove empty
I currently have a deployed app (fortworth.herokuapp.com) that I am attempting to sort movies
I currently have a small (I'm a complete beginner) project in VTK where I've
We currently have a production application that runs as a windows service. Many times
I currently have a Windows Application that uses TCP Sockets to connect users and
I currently have two XSD schemes and one is a light version of the

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.