Linq到SQL左轉外部聯(lián)接此查詢是否等效于LEFT OUTER加入?//assuming that I have a parameter named 'invoiceId' of type intfrom c in SupportCaseslet invoice = c.Invoices.FirstOrDefault(i=> i.Id == invoiceId)where (invoiceId == 0 || invoice != null) select new {
Id = c.Id
, InvoiceId = invoice == null ? 0 : invoice.Id}
3 回答

翻翻過去那場雪
TA貢獻2065條經驗 獲得超14個贊
SelectMany
DefaultIfEmpty
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};
(或者通過擴展方法)

鳳凰求蠱
TA貢獻1825條經驗 獲得超4個贊
您不需要Into語句:
var query = from customer in dc.Customers from order in dc.Orders .Where(o => customer.CustomerId == o.CustomerId) .DefaultIfEmpty() select new { Customer = customer, Order = order } //Order will be null if the left join is null
添加回答
舉報
0/150
提交
取消