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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:11:23+00:00 2026-06-17T17:11:23+00:00

I have a One-Many relation with two Entities : Order: int OrderId string OrderNumber

  • 0

I have a One-Many relation with two Entities :

Order:
int OrderId 
string OrderNumber
...

OrderItem:
int ItemId
int sequence
...

Product:
int ProductId
string ProductName

ProductType:
int ProductTypeid
string Title

One Order Has multiple OrderItems, and each OrderItem has One Product and each Product has one ProductType.

I want to write a Linq that return all orders with their items,Product,ProductType and items be sorted by sequence field. How can I write a query such as following query?

with help from Order by with Linq i wrote this query:

var myOrder= db.Orders.Include("OrderItems")
         .Where(x => x.OrderId == 3)
         .Include("OrderItems.Product") 
         .Include("OrderItems.Product.ProductType") 
         .Select(o => new {
             order = o,
             orderItems = o.OrderItems.OrderBy(i => i.sequence)
         }).FirstOrDefault();

but when it return the result, it doesn’t contain Product and ProductType data.
where is my mistake?

  • 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-06-17T17:11:25+00:00Added an answer on June 17, 2026 at 5:11 pm

    You need to put all calls to Include() first. This should work:

    var myOrder= db.Orders.Include("OrderItems")
         .Include("OrderItems.Product") 
         .Include("OrderItems.Product.ProductType") 
         .Where(x => x.OrderId == 3)
         .Select(o => new {
             order = o,
             orderItems = o.OrderItems.OrderBy(i => i.sequence)
         }).FirstOrDefault();
    

    Also, when you have .Include("OrderItems.Product.ProductType"), you do not need .Include("OrderItems") and .Include("OrderItems.Product"), because it will include OrderItems and their products on the way to including product types. It has to do this or you would be unable to navigate to them in code — what would it attach them to?

    In this case, it looks as though this might explain it: http://wildermuth.com/2008/12/28/Caution_when_Eager_Loading_in_the_Entity_Framework

    You can get around without using includes:

    var query = db.Orders
    .Where(x => x.OrderId == 3)
    .Select(o => new {
    order = o,
    orderItems = o.OrderItems.OrderBy(i => i.sequence),
    products = o.OrderItems.Select(i => new { i, i.Product, i.Product.ProductType })
    });
    

    Anything you project into your output select will be automatically eager-loaded. Eager loading like this is actually preferable in some ways, because you are only materializing what you need, rather than the full object graph. (Although in this case we’re materializing the same as what the include would.)

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

Sidebar

Related Questions

I have a One-Many relation with two Entities : Order: int OrderId string OrderNumber
I have two entities Customer and Order in a one-to-many relation. For each customer
I have two entities, A and B. A has to-Many relation with B. One
I have a many-to-one relation mapped between two entities A and B. I will
I have two entity CUSTOMER and ORDER..there is one to many relation from CUSTOMER
I have two tables in one-to-many relation: product -> orders In the Products table
I have two entities: product and tag connected by many-to-many relation. Here is a
i have two simple models that have one-many relation public class X{ public int
I have the following one-to-many relation between two objects. Parent --> IList<Child> Now, I
i have two entities named UserInfo and Department which having to-one and to-many relations

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.