开发者

linq to sql where in

I want to translate query like this:

SELECT * FROM Product WHERE Product.ID 开发者_运维技巧in (SELECT Product.ID FROM other_table)

into LINQ. I read about using the contains method but the problem is that it generates a lot of parameters for each id passed in like this:

WHERE [Product].[ID] IN (@p0, @p1)

If I had for example one bilion parameters I want to pass into my query the server won't be able to execute such a long query. Is it possible to create LINQ query in such a way that the generated SQL will be close to the original?

Thanks, Romek


If you are using large tables then IN statments are a bad idea, they are very slow. You should be doing joins.

Anyway, here is what you want;

using(dbDataContext db = new dbDataContext())
{
var result = from p in db.products
             join o in db.other_table
             on p.ID equals o.ID
             select p;
}


You should be able to use join for this.

other_Table.Join(product, ot => ot.Id, pd => pd.Id, (pd, ot) => pd);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜