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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:09:28+00:00 2026-05-12T11:09:28+00:00

I’m using EF for a set of objects and want to query one type

  • 0

I’m using EF for a set of objects and want to query one type of object with the matching entries from two other tables. This is to dump the query into an XML output. At first, used a join since in the original data each Employee always had 1+ instances of a Computer object (solution #2 below), but it’s not necessarily true.

For purpose here, imagine:

  • an Employee object,
  • each Employee has an EmployeeType (a few fixed entries) and
  • each Employee has zero or more Computer objects (typically 0-3).
  • each Computer belongs to one Employee, not all Employee has a Computer.
  • each Employee has a criteria on which the search is based (such as Division).

So I saw several possible solutions:

  1. Use the Employee.Computer.Load() within a loop, but with 10,000+ rows, that causes an enormous performance penalty.

  2. Use a join in the query, but that leaves out all Employees that do not have a Computer.

  3. Use Linq to Entities, but that seems to have the overhead of #1: when loading Computers it hits the database for EACH Employee.

  4. Use a second query (all Computers with matching Computer.Employee.Division), then in the Employee loop, add any Computer for the given employee. While implementing this, I found that just running the second query (with ToList()) the EF populates the correct Employee.Computer lists with the objects I need.

Here, #4 loads the data with only 2 database hits instead of 10k+, and the EntityFramework actually collates the objects and creates all of the relationships.

My Questions:

  • With #4, is the fact that the EF populates the Employee.Computer list something on which I can I rely? If so, can you point me to the documentation?
  • Is there a better way than #4?

UPDATE:
Well, bugger. Sorry, but I simply blew it. I was focusing on the
relationship with the “Computer” table and missed the fact that I
had an explicit Employee.EmployeeTypeReference.Load() w/o first testing
for null, so the “Computer” list was a complete non-issue.

I only found this when running some performance tests and adding
Craig’s solution to the mix. In truth, the records aren’t “Employees”
and “Computers” but abstractions, and I (conditionally) include every
field in the XML output, but they’re small: a Name, ID(PK) and ID(FK)
plus an INT on the “Employee” table. So, my presumption was that the
performance would be similar since the EF would create objects not
much heavier than the projection.

Anyway, here are the results wher the “Elapsed” time was
the difference before this query and after the resultant XML was created.

  • Case 1: Same as #2, but with Include() statements:

    list = ve.Employee.Include("Computer").Include("EmployeeType").Where(e => e.Division.ID == divId).OrderBy(e => e.Name);

    Elapsed: 4.96, 5.05

  • Case 2: Uses in-line Load():

    list = ve.Employee.Where(e => e.Division.ID == divId).OrderBy(e => e.Name);

    Elapsed: 74.62

  • Case 3: Same as #4, but with Include() statements:

    list = from e in ve.Employee.Include("Computer").Include("EmployeeType")
    where e.Division.ID == divId
    orderby e.Name select e;

    Elapsed: 4.91, 5.47

  • Case 4: Uses in-line Load():

    list = from e in ve.Employee where e.Division.ID == divId orderby e.Name select e;

    Elapsed: 74.20

  • Case 5: Use *Include(“EmployeeType”) and separate “Computer” query, let EF associate:

    elist = ve.Employee.Include("EmployeeType").Where(te => te.Division.ID == divId).OrderBy(e => e.Name).ToList();
    alist = ve.Alias.Where(a => a.Employee.Division.ID == divId).ToList();

    Elapsed: 4.50, 4.02

  • Case 6: Craig’s suggestion of projections:

    elist = from te in ve.ThesaurusEntry where te.Division.ID==divID
    orderby te.Name select new {
    ID = te.ID, Name = te.Name,
    Type = te.EmployeeType.Name,
    Freq = te.Frequency,
    Aliases = from a in te.Alias
    select new {
    ID = a.ID, Name = a.Name
    }
    };

    Elapsed: 0.73, 1.25

Conclusions

Load() is expensive, so use Include() or at least test with IsLoaded

Projection is a bit tedious, but significantly faster than EF fix-up. [with this limited testing on “narrow” tables]

  • 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-12T11:09:29+00:00Added an answer on May 12, 2026 at 11:09 am

    Rob’s solution will work (+1), but if you don’t need all of the fields in, say, Employee and Computer, I’d strongly recommend projecting instead:

    var q = from e in Context.Employees
            where e.Division.Id = divisionId
            select new
            {
                Name = e.Name,
                EmployeeType = e.EmployeeType.Description,
                ComputerIds = from c in e.Computers
                              select new 
                              {
                                  Id = c.Id
                              }
            };
    

    Here you get all you need in one query, but nothing more: All the fields you don’t need won’t be returned.

    You could probably even select into XElements and just save the resulting tree rather than manually converting to XML. I haven’t tried this, but it seems like it should work.

    Regarding #4, yes, you can rely on this, but it’s always good to check IsLoaded before calling Load().

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

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
I want use html5's new tag to play a wav file (currently only supported
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Does anyone know how can I replace this 2 symbol below from the string
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.