0

我通常对 LINQ 并不差,但这个让我很头疼。需要新鲜的头脑来看看。

我有四张桌子

Task         Account       AcctUserXref      User
-----        -------       ------------      -----
TaskID       AcctID        UserID (fk)       UserID
AcctNumber   AcctNumber    AcctID (fk)       UserName

我正在尝试根据用户名查找所有任务。这是我要转换的 SQL 语句的片段。

SELECT @cntTask = Count(TaskID) 
FROM Task tsk, Account acct, AcctUserXref acx
WHERE tsk.AcctNumber = acct.AcctNumber
AND acct.AcctID = acx.AcctID
AND acx.AcctID IN (SELECT acx2.AcctID
                   FROM AcctUserXref acx2, User usr
                   WHERE acx2.UserID = usr.UserID
                   AND usr.UserName = @UserName)

如何直接将其转换为 LINQ?或者,如果有更好的方法在 LINQ 中实现我正在尝试做的事情。

4

1 回答 1

1

我不知道为什么您的查询看起来不像:

SELECT @cntTask = Count(TaskID) 
FROM Task tsk, Account acct, AcctUserXref acx, User usr
WHERE tsk.AcctNumber = acct.AcctNumber
AND acct.AcctID = acx.AcctID
AND acx.UserID = usr.UserID
AND usr.UserName = @UserName

LINQ 等价物:

var numberOfTasks = (from u in Users
                     join au in AccountsToUsers on u.UserId equals au.UserId
                     join a in Accounts on au.AcctID equals a.AcctID
                     join t in Tasks on a.AcctNumber equals t.AcctNumber
                     where u.UserName == "NameYoureLookingFor").Count();
于 2013-04-09T17:08:35.557 回答