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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:35:57+00:00 2026-05-10T23:35:57+00:00

I have need to select a number of ‘master’ rows from a table, also

  • 0

I have need to select a number of ‘master’ rows from a table, also returning for each result a number of detail rows from another table. What is a good way of achieving this without multiple queries (one for the master rows and one per result to get the detail rows).

For example, with a database structure like below:

MasterTable:     - MasterId BIGINT     - Name NVARCHAR(100)  DetailTable:     - DetailId BIGINT     - MasterId BIGINT     - Amount MONEY 

How would I most efficiently populate the data object below?

IList<MasterDetail> data;  public class Master {     private readonly List<Detail> _details = new List<Detail>();      public long MasterId     {         get; set;     }      public string Name     {         get; set;     }      public IList<Detail> Details     {         get         {             return _details;         }     } }  public class Detail {     public long DetailId     {         get; set;     }      public decimal Amount     {         get; set;     } } 
  • 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. 2026-05-10T23:35:58+00:00Added an answer on May 10, 2026 at 11:35 pm

    Normally, I’d go for the two grids approach – however, you might also want to look at FOR XML – it is fairly easy (in SQL Server 2005 and above) to shape the parent/child data as xml, and load it from there.

    SELECT parent.*,        (SELECT * FROM child        WHERE child.parentid = parent.id FOR XML PATH('child'), TYPE) FROM parent FOR XML PATH('parent') 

    Also – LINQ-to-SQL supports this type of model, but you need to tell it which data you want ahead of time. Via DataLoadOptions.LoadWith:

    // sample from MSDN Northwnd db = new Northwnd(@'c:\northwnd.mdf'); DataLoadOptions dlo = new DataLoadOptions(); dlo.LoadWith<Customer>(c => c.Orders); db.LoadOptions = dlo;  var londonCustomers =     from cust in db.Customers     where cust.City == 'London'     select cust;  foreach (var custObj in londonCustomers) {     Console.WriteLine(custObj.CustomerID); } 

    If you don’t use LoadWith, you will get n+1 queries – one master, and one child list per master row.

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

Sidebar

Related Questions

I need a select from table which does not have column that tells when
I have a <select>. Using JavaScript, I need to get a specific <option> from
I have x number of <div> and I need to select all after n.
I need to select a row from table that has more than 5 millions
I have a master table (hereafter called SURVEY) and a detail table (hereafter called
I have the need to find the next available number in a set: select
When I have to select a number of fields from different tables: do I
I need to select emails from contact... I find android have a build-in phone
I have the following SQL Statement. I need to select the latest record for
I have a need to close a parent form from within child form 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.