我正在尝试将SQL查询转换为LINQ查询。任何帮助,将不胜感激。 这是我的SQL查询:
SELECT DISTINCT
U.UserID,
U.LoginID,
U.FirstName,
U.LastName,
U.IsActive,
U.IsApproved,
U.Email,
U.DateLastLogin,
CONVERT(VARCHAR, U.DateLastLogin, 101) AS LastLogin,
U.JobTitle,
U.AgencyName,
U.WorkPhone,
U.Fax,
U.MobilePhone,
U.Satellite,
U.Blackberry,
U.Pager,
U.Address1,
U.Address2,
U.City,
U.StateAbbr,
U.ZipCode,
G.[Name] AS GroupName,
COALESCE( B.UserID, 0 ) AS BTC
FROM dbo.Users U
INNER JOIN dbo.Users_Grantee UG ON U.UserID = UG.UserID
INNER JOIN dbo.Users_Groups UGRP ON UG.UserID = UGRP.UserID
INNER JOIN dbo.Groups G ON UGRP.GroupID = G.GroupID
--Flag for BTC Users
LEFT OUTER JOIN (
SELECT UG2.UserID
FROM dbo.Users_Groups UG2
INNER JOIN dbo.Groups G2 ON UG2.GroupID = G2.GroupID
WHERE G2.GroupClass_ID = 44
) B ON U.UserID = B.UserID
WHERE U.IsAdminUser = 0
AND U.IsActive = @IsActive
AND UG.GranteeID = @GranteeID
AND G.GroupClass_ID = 1 --Grantee GroupClass
AND (U.LastName LIKE @LastName
OR U.FirstName LIKE @FirstName
OR U.Email LIKE @Email
OR U.LoginID = @LoginID)
--Exclude PO users who have grantee access.
AND NOT EXISTS(
SELECT 1
FROM dbo.Users_Groups UG1
INNER JOIN dbo.Groups G1 ON UG1.GroupID = G1.GroupID
WHERE UG1.UserID = UG.UserID
AND G1.GroupClass_ID <> 1
AND G1.GroupClass_ID <> 44
)
答案 0 :(得分:0)
不知怎的,我用以下查询做到了:
(from u in Users
from ugrt in Users_Grantees where (u.UserID == ugrt.UserID)
from ugrp in Users_Groups where (ugrt.UserID == ugrp.UserID)
from g in Groups where (ugrp.GroupID == g.GroupID)
from ug in
(from ug2 in Users_Groups
join g2 in Groups on ug2.GroupID equals g2.GroupID
where (g2.GroupClass_ID == 44)
select new {ug2.UserID}).Where(ug=>ug.UserID == u.UserID).DefaultIfEmpty()
where
u.IsAdminUser == false
&& u.IsActive == true
&& ugrt.GranteeID == 1
&& g.GroupClass_ID == 1
orderby u.LastName
select u)
.Where (t1 => !(from ugrp1 in Users_Groups
join g1 in Groups on ugrp1.GroupID equals g1.GroupID
where (g1.GroupClass_ID != 1
&& g1.GroupClass_ID != 44)
select new {ugrp1.UserID})
.Any(t2 => t2.UserID == t1.UserID))