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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:49:22+00:00 2026-05-29T05:49:22+00:00

I am trying to write a Linq query that results in a parent entity

  • 0

I am trying to write a Linq query that results in a parent entity with one of the fields being a collection of entities of its related children. For example, I have a collection of all customers entities and a collection of all orders entities. The orders entities have a field called customerPK which contains the link to the related parent customer entity. I want to create a Linq query that joins the two collections and results in all the fields of the customer entity plus an additional field which is the collection object of all the related order entities for that specific customer entity.

  • 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-29T05:49:22+00:00Added an answer on May 29, 2026 at 5:49 am

    Hopefully this should do the trick;

    EDIT: Code updated to perform a Left Outer Join, based on this example; http://smehrozalam.wordpress.com/2009/06/10/c-left-outer-joins-with-linq/. This now includes Customers who have no orders.

    var query = from c in customers
                join o in orders on c.ID equals o.CustomerPK into joined
                from j in joined.DefaultIfEmpty()
                group j by c into g
                select new { Customer = g.Key, Orders = g.Where(x => x != null) };
    

    Note, the use of Where on the selection of the Orders grouping is so that null orders are filtered out at this point, instead of ending up with a grouping containing a single null Order for customers who don’t have orders.

    Then some example usage;

    foreach (var result in query)
    {
        Console.WriteLine("{0} (ID={1})", result.Customer.Name, result.Customer.ID);
        foreach (var order in result.Orders)
        {
            Console.WriteLine(order.Description);
        }
    }
    

    This example results in an object with two fields, the Customer and then a group of related Orders, but there’s no reason why you can’t select the individual fields of your customer object in the query as you specified in your post.

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

Sidebar

Related Questions

I'm trying to write the following LINQ-Entities query: Get a list of Questions, that
I am trying to write a linq query that will only return certain columns
I'm trying to write a linq-to-sql query using || that behaves the same way
i am trying to figure out how to write a linq query that will
i am trying to write a linq query that doesn't fire a million child
I'm trying to write Linq query on this Products table based on FacetTypes that
I'm trying to write a single Linq Query that can concat inner collections of
I am trying write a SQL query that filters a gridview by the fields
I'm trying to write a LINQ query that that simply gets the count of
I'm trying to write a linq to object query in vb.net, here is the

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.