什么是LINQ相当于SQL的“IN”关键字

时间:2022-08-24 20:49:58

How can I write below sql query in linq

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

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

4 个解决方案

#1


3  

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

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

我希望这个表单将解析为生成的SQL中的EXISTS(SELECT * ...)。

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

如果性能有很大差异,您需要对两者进行分析。

#2


6  

There is no direct equivalent in LINQ. Instead you can use contains () or any other trick to implement them. Here's an example that uses Contains:

在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;

check this link for details: in clause Linq

有关详细信息,请查看此链接:in Linq

int[] productList = new int[] { 1, 2, 3, 4 };


var myProducts = from p in db.Products
                 where productList.Contains(p.ProductID)
                select p;

#3


1  

Something similar to this

类似的东西

var partyProducts = from p in dbo.Product 
                    join pt in dbo.ProductTypeParty on p.ProductTypePartyID equal pt.PartyId 
                    where pt.PartyId = 34 
                    select p

#4


1  

You use the Contains in a Where clause.

您在Where子句中使用Contains。

Something along these lines (untested):

沿着这些方向的东西(未经测试):

var results = Product.Where(product => ProductTypeParty
                                            .Where(ptp => ptp.PartyId == 34)
                                            .Select(ptp => ptp.Id)
                                            .Contains(product.Id)
                           );

#1


3  

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

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

我希望这个表单将解析为生成的SQL中的EXISTS(SELECT * ...)。

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

如果性能有很大差异,您需要对两者进行分析。

#2


6  

There is no direct equivalent in LINQ. Instead you can use contains () or any other trick to implement them. Here's an example that uses Contains:

在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;

check this link for details: in clause Linq

有关详细信息,请查看此链接:in Linq

int[] productList = new int[] { 1, 2, 3, 4 };


var myProducts = from p in db.Products
                 where productList.Contains(p.ProductID)
                select p;

#3


1  

Something similar to this

类似的东西

var partyProducts = from p in dbo.Product 
                    join pt in dbo.ProductTypeParty on p.ProductTypePartyID equal pt.PartyId 
                    where pt.PartyId = 34 
                    select p

#4


1  

You use the Contains in a Where clause.

您在Where子句中使用Contains。

Something along these lines (untested):

沿着这些方向的东西(未经测试):

var results = Product.Where(product => ProductTypeParty
                                            .Where(ptp => ptp.PartyId == 34)
                                            .Select(ptp => ptp.Id)
                                            .Contains(product.Id)
                           );