Context
The book T-SQL Fundamentals Third Edition by Itzik Ben-Gan contains the following query in chapter 3:
SELECT C.custid, C.companyname, O.orderid
FROM Sales.Customers AS C
LEFT OUTER JOIN Sales.Orders AS O
ON C.custid = O.custid;
I've converted this to LINQ as follows:
var result =
from customer in db.Customers
join order in db.Orders
on customer.Custid equals order.Custid into Abc
from abc in Abc.DefaultIfEmpty()
select new
{
customer.Custid,
customer.Companyname,
orderid = abc == null ? -1 : abc.Orderid
};
Question
What's a good way to write the above using method syntax instead of query syntax?
I've started with this:
var result = db.Customers.Join(
db.Orders,
customer => customer.Custid,
order => order.Custid,
(customer, order) =>
new
{
customer.Custid,
customer.Companyname,
orderid = order.Orderid
}
);
However, this of course leaves out the NULL valued items.
The part I'm not clear on is how to convert the into
syntax into method syntax.
Any suggestions welcome!
Notes
The above query is in a project available here if you'd actually like to run the query yourself:
https://github.com/dharmatech/TSqlEf/blob/master/Chapter3p114/Program.cs
See the project readme for how to setup the database: