这是相当于查询到 LEFT OUTER
加入?
//假设int类型的,我有一个名为参数'invoiceId
从C在SupportCases
让发票= c.Invoices.FirstOrDefault(I => i.Id == invoiceId)
其中,(invoiceId == 0 ||发票!= NULL)
选择新
{
n = c.Id
,InvoiceId =发票== NULL? 0:invoice.Id
}
解决方案
不太 - 因为每次左的行中的左外连接将匹配0-N右行(第二个表),其中, -as你只有0-1匹配。做一个左外连接,则需要的SelectMany
和 DefaultIfEmpty
,例如:
VAR的查询=从C在db.Customers
加盟澳中db.Orders
在c.CustomerID等于o.CustomerID到SR
从x在sr.DefaultIfEmpty()
选择新{
客户id = c.CustomerID,联系人姓名= c.ContactName,
订单ID = x.OrderID == NULL? -1:x.OrderID};
(or通过扩展方法)
Is this query equivalent to a LEFT OUTER
join?
//assuming that I have a parameter named 'invoiceId' of type int
from c in SupportCases
let invoice = c.Invoices.FirstOrDefault(i=> i.Id == invoiceId)
where (invoiceId == 0 || invoice != null)
select new
{
Id = c.Id
, InvoiceId = invoice == null ? 0 : invoice.Id
}
解决方案
Not quite - since each "left" row in a left-outer-join will match 0-n "right" rows (in the second table), where-as yours matches only 0-1. To do a left outer join, you need SelectMany
and DefaultIfEmpty
, for example:
var query = from c in db.Customers
join o in db.Orders
on c.CustomerID equals o.CustomerID into sr
from x in sr.DefaultIfEmpty()
select new {
CustomerID= c.CustomerID, ContactName=c.ContactName,
OrderID = x.OrderID == null ? -1 : x.OrderID};
(or via the extension methods)