How to do multiple left outer joins in a Linq Query?

Here's a query to produce a flat result set with correct left joins var query = from cell in Cell join container in Container. Where (row => row. SerialNumber == "1102141") on cell.

CellID equals container. CellID into containers from container2 in containers.DefaultIfEmpty() join printer in Printer. Where (row => row.Name == "PG10RelWarrPrt3") on cell.

CellID equals printer. CellID into printers from printer2 in printers.DefaultIfEmpty() select new { Cell = cell, Container = container2, Printer = printer2 } You'll have to post-process the results locally to get the hierarchical shape desired If you write this post-processing code, you'll understand why linq to sql doesn't process multiple sibling collections for you To make this clearer, suppose you had 3 sibling collections If all three sibling collections were empty for some parent record, you'd have just the parent record 1 time with a bunch of nulls If all three sibling collections had 100 records for some parent record, you'd have 1 million rows each with a copy of the parent record. Every child record would be duplicated 10,000 times in the result It's always important to keep in mind with any ORM that it generates sql and gets back flat result sets, no matter what hierarchically shaped result it eventually present you with.

Here's a query to produce a flat result set with correct left joins. Var query = from cell in Cell join container in Container. Where (row => row.

SerialNumber == "1102141") on cell. CellID equals container. CellID into containers from container2 in containers.DefaultIfEmpty() join printer in Printer.

Where (row => row. Name == "PG10RelWarrPrt3") on cell. CellID equals printer.

CellID into printers from printer2 in printers.DefaultIfEmpty() select new { Cell = cell, Container = container2, Printer = printer2 }; You'll have to post-process the results locally to get the hierarchical shape desired. If you write this post-processing code, you'll understand why linq to sql doesn't process multiple sibling collections for you. To make this clearer, suppose you had 3 sibling collections.

If all three sibling collections were empty for some parent record, you'd have just the parent record 1 time with a bunch of nulls. If all three sibling collections had 100 records for some parent record, you'd have 1 million rows, each with a copy of the parent record. Every child record would be duplicated 10,000 times in the result.It's always important to keep in mind with any ORM that it generates sql and gets back flat result sets, no matter what hierarchically shaped result it eventually present you with.

It's usually wrong to use join in LINQ to SQL. Try: var query = from cell in Cell select new { Cell = cell, Containers = cell. Containers .

Where (row => row. SerialNumber == "1102141"), Printers = cell. Printers .

Where (row => row. Name == "PG10RelWarrPrt3") }.

That gives me the result set I want, but not the correct T-SQL. It generates the exact same T-SQL as the query in my post. It does a left outer join on the Container table, and then a separate query on the Printer table for every row in the Cell table.It does not do two left outer joins.

– Randy Minder Nov 24 '10 at 14:14.

This query works, but is not efficient. It does a left outer join on Container, but, for each Cell, it performs a separate query to retrieve any Printer rows, instead of also doing a left outer join on Printer. How can I change this so that it also does a left outer join on the Printer table?

BTW, I want a hierarchical result set. IOW, each Cell should have a list of containers and a list of printers.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions