Linq查询而不是包含运算符的性能问题 [英] Linq Query instead of Contains Operator for Performance issue
问题描述
我必须提取所有ID在列表中的客户
I have to Pull all customers whose ids are in the list
我有一个CustomerID的列表
I have a list of CustomerID`s
列表custidlist =新列表{1,2,3 .... etc.}();
我必须编写一个linq查询来获取所有ID在上面列表中的客户
i have to write a linq query to get all customers whose id`s are in the above list
客户列表.
var customer = db.Customers.Where(c => custidlist.包含(c.customerid));
var customers=db.Customers.Where(c=> custidlist.Contains(c.customerid));
在性能问题上使用包含不好.
Using Contains is not good in performance issue.
我们可以像这样使用COMPARE运算符
Can we use COMPARE OPERATOR LIKE THIS
var customer = db.Customers.Where(c => custidlist. Compare (c.customerid)); ????
var customers=db.Customers.Where(c=> custidlist.Compare(c.customerid)); ????
I Heard Compare最适合性能
I Heard Compare is best for Performance
推荐答案
由于这是Linq to SQL/实体,因此您的Linq Contains
查询将被转换为类似于以下内容的SQL语句:
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)
不仅不支持您的其他建议,而且您在此SQL性能方面做得更好.
Not only is your other suggestion not supported, but also you cannot do any better than this SQL performance wise.
这篇关于Linq查询而不是包含运算符的性能问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!