Linq a Sql: Múltiples uniones externas izquierdas
Estoy teniendo algunos problemas para averiguar cómo usar más de una unión externa izquierda usando LINQ a SQL. Entiendo cómo usar una unión externa izquierda. Estoy usando VB.NET. A continuación se muestra mi sintaxis 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 answers
Esto puede ser más limpio ( no necesita todas las declaraciones 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
Aquí hay otro ejemplo de unión a la izquierda
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
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-13 15:38:28
No tengo acceso a VisualStudio (estoy en mi Mac), pero usando la información de http://bhaidar.net/cs/archive/2007/08/01/left-outer-join-in-linq-to-sql.aspx parece que usted puede ser capaz de hacer algo como esto:
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 }
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-06 04:22:55
Descubrí cómo usar múltiples uniones externas izquierdas en VB.NET usando LINQ a SQL:
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-04 13:41:30
En VB.NET usando la función,
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
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-09-17 01:28:22
Creo que deberías ser capaz de seguir el método utilizado en este post. Se ve muy feo, pero creo que podrías hacerlo dos veces y obtener el resultado que quieres.
Me pregunto si este es realmente un caso en el que sería mejor usar DataContext.ExecuteCommand(...)
en lugar de convertir a linq.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2008-11-06 03:12:14
Estoy usando esta consulta linq para mi aplicación. si esto coincide con su requisito, puede referir esto. aquí me he unido (unión exterior izquierda) con 3 tablas.
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()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-08 07:59:13