LINQ to SQL是否支持t-sql“in”语句
问题描述:
我不知道如何更好地描述标题(因此可以随意更改),但是我希望在LINQ to SQL中生成以下语句的equivilent :LINQ to SQL是否支持t-sql“in”语句
select * from products where category in ('shoes','boots')
的领域将从查询字符串实质上是未来,即
string[] myfields = request.querystring["categories"].split(',');
感谢
答
是的,你可以使用(超过这一点,但为了便于说明,更安全):
db.Products.Where(product => myFields.Contains(product.Category))
又名
from product in db.Products
where myFields.Contains(product.Category)
select product
答
理论上你可以使用包含:
var productList = from product in context.Products
where myfields.contains(product.category)
select product
但是你需要测试这一点 - 我似乎记得有作为的一个bug当试图处理列表<>时使用Linq2Sql优化器,并且像这样使用数组值(它可能仅在您尝试将它们转换为IQueryable时发生)
答
正如其他人所说,是的,它使用.Contains方法。为了让可能通过Bing(或任何其他搜索引擎)到达此处的其他随机人员受益:Linq-To-Entities不支持当前版本中的.Contains方法。但是,使用简单的扩展方法,您可以这样做:
http://george.tsiokos.com/posts/2007/11/30/linq-where-x-in/