什么是LINQ等效于SQL的"IN"?关键词 [英] What is LINQ equivalent of SQL’s "IN" keyword

查看:77
本文介绍了什么是LINQ等效于SQL的"IN"?关键词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在linq中的sql查询下编写

How can I write below sql query in linq

select * from Product where ProductTypePartyID IN
(
    select Id from ProductTypeParty where PartyId = 34
)

推荐答案

除了语法变化之外,您几乎可以用相同的方式编写它.

Syntactic variations aside, you can write it in practically the same way.

from p in ctx.Product
where (from ptp in ctx.ProductTypeParty
       where ptp.PartyId == 34
       select ptp.Id).Contains(p.ProductTypePartyID)
select p

我更喜欢使用存在量词:

I prefer using the existential quantifier, though:

from p in ctx.Product
where (from ptp in ctx.ProductTypeParty
       where ptp.PartyId == 34
       && ptp.Id == p.ProductTypePartyID).Any()
select p

我希望这种形式将在生成的SQL中解析为EXISTS (SELECT * ...).

I expect that this form will resolve to an EXISTS (SELECT * ...) in the generated SQL.

如果在性能上有很大差异,您将需要同时介绍这两个方面.

You'll want to profile both, in case there's a big difference in performance.

这篇关于什么是LINQ等效于SQL的"IN"?关键词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆