sql-server - SQL 的 "IN"关键字的 LINQ 等价物是什么

标签 sql-server linq entity-framework

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

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

最佳答案

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;

查看此链接了解详细信息:in clause Linq

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


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

关于sql-server - SQL 的 "IN"关键字的 LINQ 等价物是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255205/

相关文章:

c# - Entity Framework : Loading 2nd level navigation properties

mysql - AS 遇到的错误

sql - 使用 HAVING 时出现“无效的列名”

sql-server - 经典 ASP SQL Server 数据库连接

c# - DataContext 到数据库

c# - 使用 Joiner 表从 3 个表进行 Linq 查询

c# - Linq查询问题: Distinct based on object's property

entity-framework - Entity Framework asp.net MVC 外键

c# - 为什么 Entity Framework 在不同的 AppDomain 中运行时速度明显变慢?

sql-server - 如何在 PowerBuilder Classic 12.5.2 中连接到 SQL Server 2016?