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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:45:48+00:00 2026-05-27T23:45:48+00:00

When I try to join a few tables the entity framework seems to be

  • 0

When I try to join a few tables the entity framework seems to be returning the entire table. As this table is quite large I need it to only return the rows needed.

I have three tables:

Project       - ProjectID, ProjectName
ProjectEmail  - ProjectEmailID, ProjectID, EmailID, RemovedFlag, CreatedBy
Email         - EmailID, Subject, Body 

I am trying to retrieve Email data for a particular Project.
When I do this:

using (DatabaseEntities context = new DatabaseEntities())
{
    Project proj = context.Projects.Where(p => p.ProjectID == ProjectID).FirstOrDefault();
    if (proj != null)
    {
        List<Email> projectEmails = (from pe in proj.ProjectEmails
                                     join e in context.Emails on pe.EmailID equals e.EmailID
                                     select e).ToList();
    }

}

The SQL that is generated is this:

exec sp_executesql N'SELECT TOP (1) 
[Extent1].[ProjectID] AS [ProjectID], 
[Extent1].[ProjectName] AS [ProjectName],
    -- rest of columns appear here 
FROM [dbo].[Project] AS [Extent1]
WHERE [Extent1].[ProjectID] = @p__linq__0',N'@p__linq__0 int',@p__linq__0=6

excellent, except that the second query generates this:

SELECT  [Extent1].[EmailID] AS [EmailID], 
    [Extent1].[Subject] AS [Subject],
            -- rest of columns appear here
FROM [dbo].[Email] AS [Extent1]

Email is a big table and I really don’t want to be pulling back the entire table!
Is there a better way to return the list if Emails so that the table
joins on the correct keys?

I am also confused how it knows which Emails to return, because I can’t see the first
or second query joining on the ProjectEmail table.

  • 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-27T23:45:49+00:00Added an answer on May 27, 2026 at 11:45 pm

    You missed the where condition in the query.

    List<Email> projectEmails = (from pe in proj.ProjectEmails
                                 join e in context.Emails on pe.EmailID equals e.EmailID
                                 where pe.ProjectID == proj.ProjectID
                                 select e).ToList();
    

    Edit

    You are using from pe in proj.ProjectEmails where proj.ProjectEmails is of IEnumerable type. So this LINQ query becomes a LINQ-to-objects query. That is why it loads all the Emails in context.Emails. Try

    List<Email> projectEmails = (from pe in context.ProjectEmails
                                join e in context.Emails on pe.EmailID equals e.EmailID
                                where pe.ProjectID == proj.ProjectID
                                select e).ToList();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to join 2 tables and output the result: In model: $this->db->select('*'); $this->db->from('news');
I know there are quite a few solutions for this problem but mine was
I am not sure why this behaves this way. I need to select few
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
Try running this in a .VBS file MsgBox(545.14-544.94) You get a neat little answer
I've been working on this for a few hours now and can't find any
I'm trying to write a simple custom storage system. I need only that my
OKay, I will try to make this make sense lol. Basically I have 4
This is pretty much just a simple join statemnet I believe. I've not worked
I have a thread and I need to wait for its result, like this:

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.