开发者

LINQ to EF - Simulating SQL "IN" clause

I read some great tips here regarding getting decent SQL to be generated from LINQ to EF. The LINQ query below is what I came up with. (I formulated the following LINQ query based on existing code tha开发者_如何学Ct I am now augmenting, plus tips from this forum.)

The resulting SQL being captured shows that the WHERE clause is using a bunch of successive ORs, rather than an IN. Is there a way to force this translation to IN?

      string[] circuits = circuitIDList.ToArray();
       using (var ctx = new MyEntities())
        {
            var q = from n in ctx.v_myview
                    where circuits.Contains(n.circuitid)
                    select n;

            string tSQL = q.ToTraceString();

            return q.ToList();
        }


There is no way of providing "hints" to the Linq query provider of how you want your Linq statement to be translated - this is totally up to the provider. In this case the provider might decide on a particular mapping (WHERE .. IN vs OR) for one reason or another (i.e. performance), but both are logically equivalent.

Having said that, both queries actually do result in the same execution plan (at least for SQL server), so performance wise it should not make a difference.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜