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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:14:26+00:00 2026-05-16T08:14:26+00:00

I am developing a small application using EF 4.0 and POCO. While testing my

  • 0

I am developing a small application using EF 4.0 and POCO.

While testing my application, I grew concerned about the performance of the Data Access Layer. So I fired SQL Profiler to see that when trying to retrieve a record:

ctx.Orders.Include("OrderItems").FirstOrDefault<Order>(c => c.OrderID == id);

the EF issues a SQL statement that would retrieve all records from the Orders table on the Server and as such return to DAL at which time L2E would pick one thay meet the criteria and return it.

Can this behaviour be changed.

Thanks!

Zen

  • 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-16T08:14:27+00:00Added an answer on May 16, 2026 at 8:14 am

    Try this one please:

    ctx.Orders.Include("OrderItems").Where(c => c.OrderID == id).FirstOrDefault();
    

    By the way you don’t need to look into SQL Profiler to see the generated SQL, you can do it right inside your code by writing:

    IQueryable<Order> query = ctx.Orders.Include("OrderItems")
                                        .Where(c => c.OrderID == id);
    string sql = ((ObjectQuery<Order>)query).ToTraceString();
    

    EDIT:

    Question: What if we have a function like FindOrders and we need to pass the predicate to this function?
    Answer: The code should looks like:

    public List<Order> FindOrders(Expression<Func<Order, bool>> predicate) { 
        using (DBContext ctx = new DBContext()) { 
            return ctx.Orders.Include("OrderItems").Where(predicate).ToList<Order>(); 
        } 
    } 
    
    //Calling the function:
    var order = FindOrders(c => c.OrderID == id)[0];
    

    This time, if you check your SQL Profiler you’ll see there is a where clause in the SQL that’s been submitted to SQL Server.

    Explanation:

    The reason for this “Weird behavior” is that basically when you write Where(c => c.OrderID == id), C# compiler cast your lambda expression into an Expression<Func<TSource, int, bool>> and NOT to a Func<TSource, int, bool>.

    MSDN Documentation for Queryable.Where also confirms this:

    public static IQueryable<TSource> Where<TSource>(
        this IQueryable<TSource> source,
        Expression<Func<TSource, int, bool>> predicate
    )
    



    However if you explicitly pass a Func<TSource, int, bool>> to the Where method then you are basically calling Enumerable.Where:

    public static IEnumerable<TSource> Where<TSource>(
        this IEnumerable<TSource> source,
        Func<TSource, int, bool> predicate
    )
    



    And as we know IEnumerable.Where is "LINQ to Objects" implementation and NOT "LINQ to Entities" which means onces you reach to your IEnumerable.Where call, the ObjectQuery run the initial query (ctx.Orders.Include("OrderItems")) and gives the results to the IEnumerable.Where so that it will filter it out for you on the Client Side.

    On the other hand the call with Queryable.Where (ctx.Orders.Include("OrderItems").Where(c => c.OrderID == id).FirstOrDefault()) will not be executed until it reach to the point that we call FirstOrDefault() function which means the Queryable.Where is then translated into native SQL along with the rest of the query and will be passed to the SQL Server, hence you see the Where clause on the SQL statement which definitely is the desired runtime behavior.

    By the way, don't forget to import this namespace to your class file:

    using System.Linq.Expressions;
    

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

Sidebar

Ask A Question

Stats

  • Questions 532k
  • Answers 532k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The char type is allowed to be signed, and conversion… May 17, 2026 at 12:11 am
  • Editorial Team
    Editorial Team added an answer Figured it out. Had to modify my configuration as follows:… May 17, 2026 at 12:11 am
  • Editorial Team
    Editorial Team added an answer This 'pattern' is how you do it in AspectJ. Declare… May 17, 2026 at 12:11 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am developing a small desktop application using C#.NET and MS-Access . I don't
I'm about to start developing a small Windows application using Microsoft Visual C# 2008
I'm developing a small application using Blend, basically what I have in my application
I'm thinking of developing a small application using IronPython, however I want to distribute
I'm developing a small web application using Microsoft Silverlight 3. I'm using Microsoft Expressin
I am developing a small web application using cherrypy and I would like to
I'm currently developing a small WPF application using a file database (SQLCe). Since I'm
I am developing a small utility application emailafriend using Spring. The basic functionality is
I have question I'm developing small application to desktop in win Forms. I'm using
I am developing a small Ruby-on-rails application. I am using 'roo' gem to open

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.