Linq:SqlFunctions.PatIndex与string.包含字符串比较 [英] Linq: SqlFunctions.PatIndex vs string.Contains for string comparisson

查看:88
本文介绍了Linq:SqlFunctions.PatIndex与string.包含字符串比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

针对同一查询的以下哪个版本会表现更好:

Which of the following versions for the same query will perform better:

版本1(字符串.包含):

var query = db.Products
    .Where( p => p.Description.Contains( description ) );

版本2(SqlFunctions.PatIndex):

var query = db.Products
    .Where( p => SqlFunctions.PatIndex("%" + description + "%",p.Description) > 0  );

推荐答案

我认为版本1在理论上运行得更快.

I believe version 1 runs faster theoretically.

原因:

  1. 两个版本最终都会转换为SQL查询.
  2. 版本1转换为带有where子句且具有'like'运算符的查询
  3. 版本2转换为带有where子句且具有"PatIndex"功能的查询.
  4. 我们知道,与大型数据集中的纯'like'运算符相比,SQL中的函数将需要更长的时间才能返回结果.

这篇关于Linq:SqlFunctions.PatIndex与string.包含字符串比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆