pra*_*ech 6 linq sql-server entity-framework
我怎样才能在linq中编写下面的sql查询
select * from Product where ProductTypePartyID IN
(
select Id from ProductTypeParty where PartyId = 34
)
Run Code Online (Sandbox Code Playgroud)
LINQ中没有直接的等价物.相反,您可以使用contains()或任何其他技巧来实现它们.这是一个使用的示例Contains:
String [] s = new String [5];
s [0] = "34";
s [1] = "12";
s [2] = "55";
s [3] = "4";
s [4] = "61";
var result = from d in context.TableName
where s.Contains (d.fieldname)
select d;
Run Code Online (Sandbox Code Playgroud)
查看此链接了解详细信息:in Linq
int[] productList = new int[] { 1, 2, 3, 4 };
var myProducts = from p in db.Products
where productList.Contains(p.ProductID)
select p;
Run Code Online (Sandbox Code Playgroud)
撇开句法变化不谈,您可以用几乎相同的方式编写它。
from p in ctx.Product
where (from ptp in ctx.ProductTypeParty
where ptp.PartyId == 34
select ptp.Id).Contains(p.ProductTypePartyID)
select p
Run Code Online (Sandbox Code Playgroud)
不过,我更喜欢使用存在量词:
from p in ctx.Product
where (from ptp in ctx.ProductTypeParty
where ptp.PartyId == 34
&& ptp.Id == p.ProductTypePartyID).Any()
select p
Run Code Online (Sandbox Code Playgroud)
我希望此表单将解析为EXISTS (SELECT * ...)生成的 SQL 中的 。
如果性能存在很大差异,您将需要对两者进行概要分析。
| 归档时间: |
|
| 查看次数: |
7685 次 |
| 最近记录: |