2009-08-19 58 views
3

我不知道如何更好地描述標題(因此可以隨意更改),但是我希望在LINQ to SQL中生成以下語句的equivilent :LINQ to SQL是否支持t-sql「in」語句

select * from products where category in ('shoes','boots') 

的領域將從查詢字符串實質上是未來,即

string[] myfields = request.querystring["categories"].split(','); 

感謝

回答

15

是的,你可以使用(超過這一點,但爲了便於說明,更安全):

db.Products.Where(product => myFields.Contains(product.Category)) 

又名

from product in db.Products 
where myFields.Contains(product.Category) 
select product 
0

理論上你可以使用包含:

var productList = from product in context.Products 
    where myfields.contains(product.category) 
    select product 

但是你需要測試這一點 - 我似乎記得有作爲的一個bug當試圖處理列表<>時使用Linq2Sql優化器,並且像這樣使用數組值(它可能僅在您嘗試將它們轉換爲IQueryable時發生)

1

正如其他人所說,是的,它使用.Contains方法。爲了讓可能通過Bing(或任何其他搜索引擎)到達此處的其他隨機人員受益:Linq-To-Entities不支持當前版本中的.Contains方法。但是,使用簡單的擴展方法,您可以這樣做:
http://george.tsiokos.com/posts/2007/11/30/linq-where-x-in/