如何将此 SQL 语句转换为 LINQ to Entities。我找不到用 WHERE 子句联合表的方法
SQL:
Declare @DID int, @key varchar(50)
SET @DID = 3
SET @key = ''
SELECT TBL.GID, TBL.GName, TBL.DID FROM
(
SELECT TOP 1 (@DID*10000) AS [GID], '' AS [GName], @DID AS [DID]
FROM dbo.Employees E
WHERE E.DepartmentID=@DID
AND (E.GroupID IS NULL OR E.GroupID = 0)
UNION ALL
SELECT G.GroupID AS [GID], G.GroupName AS [GName], G.DepartmentID AS [DID]
FROM dbo.Groups G
) TBL
WHERE TBL.DID IN (@DID)
AND TBL.GName IN
(
SELECT (CASE WHEN E.GroupID = 0 or E.GroupID IS NULL THEN ''
ELSE (SELECT G.GroupName FROM Groups G WHERE G.GroupID=E.GroupID) END)
FROM Employees E
WHERE E.DepartmentID = @DID
AND (E.FirstName + ' ' + E.LastName) LIKE '%' + @key + '%'
)
ORDER BY TBL.GName DESC
LINQ 到实体:
var Details = (from a in Context.Employees
where a.DepartmentID == DepartmentID
&& (a.GroupID == null || a.GroupID == 0)
select new
{
GID = Convert.ToInt32(DepartmentID * 10000),
GName = "",
DID = a.DepartmentID
}
).Concat(
from a in Context.Groups
select new
{
GID = a.GroupID,
GName = a.GroupName,
DID = DepartmentID
}
);