jim crown
jim crown

Reputation: 483

SQL to Linq translation - is it even possible

I have a SQL query as follows that I am struggling to translate into equivalent Linq.

SELECT * 
FROM Product A
WHERE NOT EXISTS
(
    SELECT * FROM Vendors B JOIN ThirdPartyProduct C 
    ON B.ProductId = C.ExternalProductId 
    WHERE A.ProductCode = C.PrdouctCode AND C.SupportCode
)

I know I can use Any for doing the non-exist part of query, however, since I have a join also, I don't know how to work it out in the Linq. Any help here will be highly appreciated.

Upvotes: 0

Views: 133

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205629

LINQ also supports inline subqueries, so the translation could be basically one to one - just the typical LINQ select going last and parameterless Any going after the subquery. In other words, SQL NOT EXISTS (subquery) translates to LINQ !(subquery).Any().

Something like this:

from a in db.Product
where !(from b in db.Vendors
        join c in db.ThirdPartyProduct
        on b.ProductId equals c.ExternalProductId
        where a.ProductCode == c.ProductCode && c.SupportCode
        select b).Any()
select a;

Upvotes: 2

Related Questions