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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:26:02+00:00 2026-05-17T22:26:02+00:00

I had a LINQ query that loads a hierarchy of objects like the following.

  • 0

I had a LINQ query that loads a hierarchy of objects like the following.

Query #1

var result = db.Orders
               .Include("Customer")
               // many other .Include() here
               .FirstOrDefault(x => x.Customer.CustomerId == 1 &&
                                    x.OrderId == orderId);

I was having MAJOR performance problem with it.
The CPU usage was near 100% and memory usage was very high.

And I tweaked it to the following and the performance problem was fixed.

Query #2

var result = db.Orders
               .Include("Customer")
               // many other .Include() here
               .Where(x => x.Customer.CustomerId == 1 &&
                           x.OrderId == orderId)
               .FirstOrDefault();

I just want to confirm my suspicion.
Query #1 is probably looping through all my records in memory looking for a matching record
vs
Query #2 filters the records on the Database and then getting the first record only.

Is that why the Query #1 has performance problems?

Just to be safe, do I need to use the .Select(x => x) before the .FirstOrDefault()?

Query #3

var result = db.Orders
               .Include("Customer")
               // many other .Include() here
               .Where(x => x.Customer.CustomerId == 1 &&
                           x.OrderId == orderId)
               .Select(x => x)
               .FirstOrDefault();
  • 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-17T22:26:02+00:00Added an answer on May 17, 2026 at 10:26 pm

    No, they both should result in a same SQL query when being executed. You can prove it by looking into SQL Profiler and see what is the exact SQL being submitted from EF in both cases. Your performance optimization should have been caused by some other factors. Here is why:

    Include method returns an ObjectQuery<T>:

    public class ObjectQuery<T> : ObjectQuery, IOrderedQueryable<T>, 
                                  IQueryable<T>, IEnumerable<T>, 
                                  IOrderedQueryable, IQueryable, 
                                  IEnumerable, IListSource
    

    Which means its FirstOrDefault method comes with 2 overloads:

    // Defined by Enumerable:
    FirstOrDefault(Func<T, Boolean>)
    
    // Defined by Queryable:
    FirstOrDefault(Expression<Func<T, Boolean>>)
    

    When you code .FirstOrDefault(x => x.Customer.CustomerId == 1 compiler will go into a process called Overload Resolution to infer the type of the lambda expression x => x.Customer.CustomerId == 1 since it is convertible to the type of both overload’s parameter types.

    Compiler will use an algorithm (that I am still trying to find in C# Language Specification!), figure out that converting the lambda to the Expression<Func<T, Boolean> is a better conversion than to Func<T, Boolean> so pick the IQueryable overload.

    Therefore, you’ll see the predicate in the generated SQL when observing it in the SQL Profiler.

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

Sidebar

Related Questions

I had a Linq query against my Entity Framework model that was something like:
I had the following Linq code: var allRequests = model.GetAllRequests(); var unsatisifedRequests = (from
I'm quite new to LINQ. Suppose that I had the following table: Incident ID
Had a problem in a complex linq query so I simplified it in LINQPad:
Imagine the following (simplified) database layout: We have many holiday records that relate to
If I have something like: var query = from children in _data.Children where children.ChildId
How do I filter my Linq-to-SQL query so that it only shows records that
I have a Linq to SQL query that was working just fine with SQL
I am using LINQ to query a generic dictionary and then use the result
Assuming I had a collection like this.. var list = new List<Item> { new

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.