C# LINQ to SQL - 一对多关系
C# LINQ to SQL - one to many relationship
我在 LINQ 中创建此查询时遇到问题:
USE Northwind
GO
SELECT emp.FirstName, emp.LastName, tr.TerritoryDescription, reg.RegionDescription
FROM Employees emp
INNER JOIN EmployeeTerritories empt ON empt.EmployeeID = emp.EmployeeID
INNER JOIN Territories tr ON tr.TerritoryID = empt.TerritoryID
INNER JOIN Region reg ON reg.RegionID = tr.RegionID
这是我目前的创作:
var query = await context
.Employees
.Select(x => new
{
x.FirstName,
x.LastName,
TerritoryId = x.EmployeeTerritories. //cannot access properties
})
.ToListAsync();
但我无法轻松访问 EmployeeTerritories
属性,因为它不是 1:1 关系。我接受这个问题的线索和完整的解决方案。
编辑
这就是我目前拥有的:
var query = await context
.Employees
.Select(x => new
{
x.FirstName,
x.LastName,
TerritoryDescription = x.EmployeeTerritories
.Select(et => et.Territory.TerritoryDescription)
.ToList(),
RegionDesicription = x.EmployeeTerritories
.Select(et => et.Territory.Region.RegionDescription)
.ToList()
})
.ToListAsync();
有什么办法可以优化吗? RegionDescription
现在是一个包含一个元素的 list
,但我不知道如何更好地做到这一点。
尝试这样的事情(假设你有对应关系):
var query = await context
.Employees
.Select(x => new
{
x.Employee.FirstName,
x.Employee.LastName,
TerritoryDescription = x.EmployeeTerritories
.Select(et => et.Territory.TerritoryDescription)
.ToList(),
})
.ToListAsync();
UPD
要在您的特定情况下展平,您可以使用 @dhrumil shah 发布的解决方案(它更通用)或尝试类似的方法,如果您在上下文中设置了 EmployeeTerritories
:
var query = await context
.EmployeeTerritories
.Select(et => new
{
et.Employee.FirstName,
et.Employee.LastName,
et.Territory.TerritoryDescription,
et.Territory.Region.RegionDescription
})
.ToListAsync();
(from emp in context.Employees
join empt in context.EmployeeTerritories
on emp.EmployeeID equals empt.EmployeeID
join tr in context.EmployeeTerritories
on empt.TerritoryID equals tr.EmployeeID
join reg in context.Region
on reg.RegionID equals tr.RegionID
select new {
emp.FirstName,
emp.LastName,
tr.TerritoryDescription,
reg.RegionDescription
}).ToList();
我在 LINQ 中创建此查询时遇到问题:
USE Northwind
GO
SELECT emp.FirstName, emp.LastName, tr.TerritoryDescription, reg.RegionDescription
FROM Employees emp
INNER JOIN EmployeeTerritories empt ON empt.EmployeeID = emp.EmployeeID
INNER JOIN Territories tr ON tr.TerritoryID = empt.TerritoryID
INNER JOIN Region reg ON reg.RegionID = tr.RegionID
这是我目前的创作:
var query = await context .Employees .Select(x => new { x.FirstName, x.LastName, TerritoryId = x.EmployeeTerritories. //cannot access properties }) .ToListAsync();
但我无法轻松访问 EmployeeTerritories
属性,因为它不是 1:1 关系。我接受这个问题的线索和完整的解决方案。
编辑
这就是我目前拥有的:
var query = await context .Employees .Select(x => new { x.FirstName, x.LastName, TerritoryDescription = x.EmployeeTerritories .Select(et => et.Territory.TerritoryDescription) .ToList(), RegionDesicription = x.EmployeeTerritories .Select(et => et.Territory.Region.RegionDescription) .ToList() }) .ToListAsync();
有什么办法可以优化吗? RegionDescription
现在是一个包含一个元素的 list
,但我不知道如何更好地做到这一点。
尝试这样的事情(假设你有对应关系):
var query = await context
.Employees
.Select(x => new
{
x.Employee.FirstName,
x.Employee.LastName,
TerritoryDescription = x.EmployeeTerritories
.Select(et => et.Territory.TerritoryDescription)
.ToList(),
})
.ToListAsync();
UPD
要在您的特定情况下展平,您可以使用 @dhrumil shah 发布的解决方案(它更通用)或尝试类似的方法,如果您在上下文中设置了 EmployeeTerritories
:
var query = await context
.EmployeeTerritories
.Select(et => new
{
et.Employee.FirstName,
et.Employee.LastName,
et.Territory.TerritoryDescription,
et.Territory.Region.RegionDescription
})
.ToListAsync();
(from emp in context.Employees
join empt in context.EmployeeTerritories
on emp.EmployeeID equals empt.EmployeeID
join tr in context.EmployeeTerritories
on empt.TerritoryID equals tr.EmployeeID
join reg in context.Region
on reg.RegionID equals tr.RegionID
select new {
emp.FirstName,
emp.LastName,
tr.TerritoryDescription,
reg.RegionDescription
}).ToList();