开发者

Linq Query instead of Contains Operator for Performance issue

I have to Pull all customers whose ids are in the list

I have a list of CustomerID`s

List custidlist=new List{1,2,3....etc.}();

i have to write a linq query to get all customers whose id`s are in the above list

custidlist.

var customers=db.Customers.Where(c=> custidlist.Contains(c.customerid));

Using Contains is not good in performance issue.

Can we use COMPARE OPERATOR LIKE THIS

var customers=db.Cu开发者_JAVA技巧stomers.Where(c=> custidlist.Compare(c.customerid)); ????

I Heard Compare is best for Performance


Since this is Linq to SQL / Entities your Linq Contains query will be translated to a SQL statement roughly like:

select * from Customers where customerId in (1,2,3)

Not only is your other suggestion not supported, but also you cannot do any better than this SQL performance wise.


When you write a Contains query in Linq to SQL it iwll be fired as an in query in SQL and running your query on the databse should be the fastest..

one caveats to this though is to remember in query might have a limit on the number of entities I think its around 2000+ in sql server and the way around this would be to batch your query.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜