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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:10:33+00:00 2026-05-13T17:10:33+00:00

I have an entity framework 3.5 project where I’m using TPH inheritence. Two of

  • 0

I have an entity framework 3.5 project where I’m using TPH inheritence. Two of my concrete types are in their own DAO class and contain a selector that projects the Entity Class into a DTO. However, both of these concrete classes have similar relations with another table which I use a let statement to identity clearly. My question is, can I somehow refactor this let statement because as I create more inherited concrete classes I feel I’m violating DRY.

StudentDAO.cs:

var myDTO = from x in context.Students
let address = (from a in x.addresses where a.active == true select a).FirstOrDefault()
select new StudentDTO { id = x.id, studentname = x.studentname, address = a.address};

ProfessorDAO.cs:

var myDTO = from x in context.Professors
let address = (from a in x.addresses where a.active == true select a).FirstOrDefault()
select new ProfessorDTO { id = x.id, professorname = x.studentname, address = a.address};

so is there a way I can refactor the address out of both of these queries?

  • 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-13T17:10:33+00:00Added an answer on May 13, 2026 at 5:10 pm

    Short answer: I’m afraid there isn’t much you could easily do. The problem is that you need to parameterize the query. The query however needs to be represented as an expression tree (so that it can be translated to SQL or whatever EF uses). Unfortunately, C# don’t provide any elegant ways for composing expression trees.

    Long answer: Some time ago, I wrote a C# article that explains how to compose queries (using some tricks) – you can take expression tree and splice it into a parameterized query. So, in principle you could use the techniques from the article to create a method that takes the changing parts of the expression tree and then compose the query.

    I’ll use the types used in LINQ to SQL, because I’m more familiar with it, but I believe the principle should be the same:

    IQueryable<R> GetInfo<T>(IQueryable<T> source, // e.g. context.Students
      Expression<Func<T, IQueryable<Address>>> getAddr, // x => x.adresses
      Expression<Func<T, Address, R>> getRes // (x, a) => new StudentDTO { ... }
      return
        from x in source.AsExpandable()
        let address = (from a in getAddr(x).Expand() where a.active == true 
                       select a).FirstOrDefault() 
        select getRes(x, a).Expand();
    }
    
    // The use would look like this
    var res = GetInfo(context.Professors, x => x.addresses, (x, a) => new 
      ProfessorDTO { id = x.id, professorname = x.studentname, address = a.address }); 
    

    So a summary is that C# and LINQ do not provide any built-in support that would make it easier. However, with some effort you can write query that is parameterized by some parts of the expression tree. In your case, it makes the code a little shorter and less repetitive, but it makes it a lot more complex (and you also need to use library like the one I referenced that provides AsExpandable and Expand).

    In this case I’m afraid the it’s not worth the effort. But it would be nice if C# v(Next)^x provided some more elegant support for things like this 🙂

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

Sidebar

Related Questions

I have an Entity Framework v1 project. I have two entities (Roles and Permissions),
I'm using Entity Framework 4 in my project. The Framework has created its own
I have a problem with the following Linq query using Entity Framework: from o
I am using Entity Framework with my website. To improve performance, I have started
So, I am using the Linq entity framework. I have 2 entities: Content and
Using NHibernate.Mapping.Attributes, I have a entity class with something like: [Class] public class EntityA
I have an Entity Framework project, and it imports a read-only view as one
I have converted an Entity framework project to use POCO objects by removing the
I have the following table structure, which is imported into an Entity Framework project:
I am busy with an MVC 2 project. I have my entity framework 4

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.