开发者

How to perform a select using a WHERE NOT EXISTS [duplicate]

This question already has answers here: Closed 10 years ago.
开发者_如何学编程

Possible Duplicate:

LINQ - Where not exists

I'm using LINQ2SQL and I want to compare two tables and select all rows that are missing from one tables (based upon one of the column values).

In standard SQL I would write this as:

SELECT 
FirstName,
LastName,
RefId,
Email
FROM
Users_ActiveDirectory AS ADU
WHERE
NOT EXISTS
(
SELECT 
U.RefId
FROM
Users AS U
WHERE
U.RefID = ADU.RefId
)

However I'm not sure how to achieve the same result using LINQ2SQL?


Linq has a .Any() function which returns true if the sequence contains anything so something like...

from ADU in Users_ActiveDirectory
where !((from U in Users where U.RefID == ADU.RefId).Any())
select new
{
    ADU.FirstName, 
    ADU.LastName, 
    ADU.RefId, 
    ADU.Email 
}

Not tested, not sure if you need the extra brackets or if you'd have to do == false instead...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜