vb.net Linq to Sql:多个左外连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/267488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linq to Sql: Multiple left outer joins
提问by Bryan Roth
I'm having some trouble figuring out how to use more than one left outer join using LINQ to SQL. I understand how to use one left outer join. I'm using VB.NET. Below is my SQL syntax.
我在弄清楚如何使用 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
回答by Amir
This may be cleaner (you dont need all the into
statements):
这可能更干净(您不需要所有into
语句):
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
Here is another left join example
这是另一个左连接示例
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
}
回答by tvanfosson
Don't have access to VisualStudio (I'm on my Mac), but using the information from http://bhaidar.net/cs/archive/2007/08/01/left-outer-join-in-linq-to-sql.aspxit looks like you may be able to do something like this:
无法访问 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 }
回答by Bryan Roth
I figured out how to use multiple left outer joins in VB.NET using LINQ to SQL:
我想出了如何使用 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
回答by Mitul
In VB.NET using Function,
在 VB.NET 中使用函数,
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
回答by Jon Norton
I think you should be able to follow the method used in thispost. It looks really ugly, but I would think you could do it twice and get the result you want.
我想你应该能够按照使用的方法这个职位。它看起来真的很丑,但我认为你可以做两次并得到你想要的结果。
I wonder if this is actually a case where you'd be better off using DataContext.ExecuteCommand(...)
instead of converting to linq.
我想知道这是否真的是您最好使用DataContext.ExecuteCommand(...)
而不是转换为 linq 的情况。
回答by Iam ck
I am using this linq query for my application. if this match your requirement you can refer this. here i have joined(Left outer join) with 3 tables.
我在我的应用程序中使用了这个 linq 查询。如果这符合您的要求,您可以参考这个。在这里,我加入了(左外连接)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()