如何将以下SQL查询转换为Lambda表达式?

时间:2014-01-21 17:13:51

标签: linq lambda

我有以下SQL query如何转换为lambda表达式

select * from ContractItems where ID in (SELECT distinct contractItemId from ContractPackageItems where contractPackageId in (SELECT ID from ContractPackage where ContractID = 680))

从上面的查询我需要知道行是否存在。如果存在行,则返回true。

-TIA

--- ---更新 这是我得到的,但它没有工作

(from contractItem in _entities.ContractItems
 where contractItem.ID == (from contractPackageItems in _entities.ContractPackageItems
 where contractPackageItems.ContractPackageID ==  (from contractPackage in _entities.ContractPackages where contractPackage.ContractID == contractId select contractPackage.ID) select contractPackageItems.ContractItemId).Distinct()).Any();

1 个答案:

答案 0 :(得分:1)

这不会做你想要的吗?

var results = (from ci in _entities.contractItems
               join cpi in _entities.contractPackageItems on ci.ID equals cpi.contractItemId
               join cp in _entities.contractPackage on cpi.contractPackageId equals cp.ID
               where cp.ContractID = 680
               select ci).Any();