.net - LINQ to SQL 左外连接

标签 .net database linq linq-to-sql

此查询是否等同于 LEFT OUTER 联接?

//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
}

最佳答案

您不需要 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

是的,上面的查询确实创建了一个 LEFT OUTER 连接。

链接到处理多个左连接的类似问题: Linq to Sql: Multiple left outer joins

关于.net - LINQ to SQL 左外连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/700523/

相关文章:

C#字节操作优化: how to get dword from byte[] array

c# - 序列化 XAML 的 ConcurrentBag

.net - 是否有用于 Kinect 的 .NET 库来为 PC(不是 XBOX)开发 Kinect 应用程序

php - 将列链接到 MySQL 数据库中的行

.net - 处理 XElement null 值

c# - Linq 查询耗时过长

c# - 通过 System.Linq.Expressions.Expression 创建表达式时,LINQ to Entities 无法识别方法 'Boolean HasFlag(System.Enum)'

.net - MSBuild 项目引用 :private ("Copy Local") - what are the allowed values and behaviour?

php - 如何使用 php 将可变数量的值插入数据库

java - 错误 XJ001 : Java exception: ': java.lang.StackOverflowError'