As a rule, I would use a two-grid approach, however you can also look at FOR XML - it is quite simple (in SQL Server 2005 and above) to form 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 say what 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 are not using LoadWith , you will get n + 1 queries - one master and one child list on each line.
source share