I'm trying to construct my linq-to-sql expression so that it only generates a single SQL database query. The query involves two nested selects, a simplifed verion is:
var query = from person in People
where person.ID == 1234
select new PersonDetails()
{
ID = person.ID,
FirstName = person.FirstName,
LastName = person.LastName,
Addresses = from address in Addresses
where address.PersonID == person.ID
select address
PhoneNumbers = from number in PhoneNumbers
where number.PersonID == person.ID
select number
}
The expression will result in two SQL queries being executed. The first one joins the Person and PhoneNumbers tables and pulls the required information from them. The second query is a simple select from the Addresses table. If I comment out the phone numbers property then only a single query which joins Person and Addresses gets executed so the L2S engine does recognise both the relationships.
Does anyone have any idea how I could construct the LINQ expression so that linq-to-sql would only generate a single SQL query?