Linq to Sql:多个左外连接

我遇到了一些麻烦,弄清楚如何使用LINQ to SQL使用多个左外连接。 我明白如何使用一个左外连接。 我正在使用VB.NET。 以下是我的SQL语法。

T-SQL

SELECT o.OrderNumber, v.VendorName, s.StatusName FROM Orders o LEFT OUTER JOIN Vendors v ON v.Id = o.VendorId LEFT OUTER JOIN Status s ON s.Id = o.StatusId WHERE o.OrderNumber >= 100000 AND o.OrderNumber <= 200000 

6 Solutions collect form web for “Linq to Sql:多个左外连接”

这可能更清洁( 你不需要所有的陈述 ):

 var query = from order in dc.Orders from vendor in dc.Vendors .Where(v => v.Id == order.VendorId) .DefaultIfEmpty() from status in dc.Status .Where(s => s.Id == order.StatusId) .DefaultIfEmpty() select new { Order = order, Vendor = vendor, Status = status } //Vendor and Status properties will be null if the left join is null 

这是另一个左连接的例子

 var results = from expense in expenseDataContext.ExpenseDtos where expense.Id == expenseId //some expense id that was passed in from category // left join on categories table if exists in expenseDataContext.CategoryDtos .Where(c => c.Id == expense.CategoryId) .DefaultIfEmpty() // left join on expense type table if exists from expenseType in expenseDataContext.ExpenseTypeDtos .Where(e => e.Id == expense.ExpenseTypeId) .DefaultIfEmpty() // left join on currency table if exists from currency in expenseDataContext.CurrencyDtos .Where(c => c.CurrencyID == expense.FKCurrencyID) .DefaultIfEmpty() select new { Expense = expense, // category will be null if join doesn't exist Category = category, // expensetype will be null if join doesn't exist ExpenseType = expenseType, // currency will be null if join doesn't exist Currency = currency } 

无法访问VisualStudio(我在我的Mac上),但使用http://bhaidar.net/cs/archive/2007/08/01/left-outer-join-in-linq-to -sql.aspx它看起来像你可能可以做这样的事情:

 var query = from o in dc.Orders join v in dc.Vendors on o.VendorId equals v.Id into ov from x in ov.DefaultIfEmpty() join s in dc.Status on o.StatusId equals s.Id into os from y in os.DefaultIfEmpty() select new { o.OrderNumber, x.VendorName, y.StatusName } 

我想出了如何使用LINQ to SQL在VB.NET中使用多个左外连接:

 Dim db As New ContractDataContext() Dim query = From o In db.Orders _ Group Join v In db.Vendors _ On v.VendorNumber Equals o.VendorNumber _ Into ov = Group _ From x In ov.DefaultIfEmpty() _ Group Join s In db.Status _ On s.Id Equals o.StatusId Into os = Group _ From y In os.DefaultIfEmpty() _ Where o.OrderNumber >= 100000 And o.OrderNumber <= 200000 _ Select Vendor_Name = x.Name, _ Order_Number = o.OrderNumber, _ Status_Name = y.StatusName 

在VB.NET中使用Function,

 Dim query = From order In dc.Orders From vendor In dc.Vendors.Where(Function(v) v.Id = order.VendorId).DefaultIfEmpty() From status In dc.Status.Where(Function(s) s.Id = order.StatusId).DefaultIfEmpty() Select Order = order, Vendor = vendor, Status = status 

我认为你应该能够遵循这篇文章中使用的方法。 它看起来非常丑陋,但我认为你可以做两次,得到你想要的结果。

我不知道这是否真的是你最好使用DataContext.ExecuteCommand(...)而不是转换为linq的情况。

我正在使用这个linq查询为我的应用程序。 如果这符合你的要求,你可以参考这个。 在这里我已经join了(左外连接)3张桌子。

  Dim result = (From csL In contractEntity.CSLogin.Where(Function(cs) cs.Login = login AndAlso cs.Password = password).DefaultIfEmpty From usrT In contractEntity.UserType.Where(Function(uTyp) uTyp.UserTypeID = csL.UserTyp).DefaultIfEmpty ' <== makes join left join From kunD In contractEntity.EmployeeMaster.Where(Function(kunDat) kunDat.CSLoginID = csL.CSLoginID).DefaultIfEmpty Select New With { .CSLoginID = csL.CSLoginID, .UserType = csL.UserTyp}).ToList() 
  • 如何快速在我的EDMX模型中find特定的表格?
  • “使用”语句如何从C#转换为VB?
  • 如何使String.Contains不区分大小写?
  • 如何从.NET执行SSIS包?
  • 如何将VB项目转换为C#项目
  • XSLT是否具有Split()函数?
  • 如何使用Google图表API在X轴上使用date?
  • 使用存储在二维数组中的相关数据
  • VB.NET - IIF(,,) - 这两个“双方”进行了评估。 我应该注意什么情况?
  • 可以在.Net 4.0中使用Tuple的实际示例?
  • VB.NET中的类与模块