http://www.cnblogs.com/freeliver54/archive/2009/09/05/1560815.html

http://www.cnblogs.com/chen1388/archive/2010/03/12/1684450.html

http://www.cnblogs.com/hantianwei/archive/2011/04/08/2009768.html

本文转自:http://blogs.microsoft.co.il/blogs/bursteg/archive/2007/10/16/linq-to-sql-like-operator.aspx
原文如下:

As a response for customer's question, I decided to write about using Like Operator in Linq to SQL queries.

Starting from a simple query from Northwind Database;

var query = from c in ctx.Customers

where c.City == "London"

select c;

The query that will be sent to the database will be:

SELECT CustomerID, CompanyName, ...
FROM    dbo.Customers
WHERE  City = [London]

There are some ways to write a Linq query that reaults in using Like Operator in the SQL statement:

1. Using String.StartsWith or String.Endswith

Writing the following query:

var query = from c in ctx.Customers

where c.City.StartsWith("Lo")

select c;

will generate this SQL statement:

SELECT CustomerID, CompanyName, ...
FROM    dbo.Customers
WHERE  City LIKE [Lo%]

which is exactly what we wanted. Same goes with String.EndsWith.

But, what is we want to query the customer with city name like "L_n%"? (starts with a Capital 'L', than some character, than 'n' and than the rest of the name). Using the query

var query = from c in ctx.Customers

where c.City.StartsWith("L") && c.City.Contains("n")

select c;

generates the statement:

SELECT CustomerID, CompanyName, ...
FROM    dbo.Customers
WHERE  City LIKE [L%]
AND      City LIKE [%n%]

which is not exactly what we wanted, and a little more complicated as well.

2. Using SqlMethods.Like method

Digging into System.Data.Linq.SqlClient namespace, I found a little helper class called SqlMethods, which can be very usefull in such scenarios. SqlMethods has a method called Like, that can be used in a Linq to SQL query:

var query = from c in ctx.Customers

where SqlMethods.Like(c.City, "L_n%")

select c;

This method gets the string expression to check (the customer's city in this example) and the patterns to test against which is provided in the same way you'd write a LIKE clause in SQL.

Using the above query generated the required SQL statement:

SELECT CustomerID, CompanyName, ...
FROM    dbo.Customers
WHERE  City LIKE [L_n%]

SqlMethods.Like还有一个参数,叫escape Character,其将会被翻译成类似下面的语句。

SELECT columns FROM table WHERE 
column LIKE '%\%%' ESCAPE '\'
escape 是因为某字段中含有特殊字符,比如%,_ [ ]这些被用作通配符的。这时就要用到Escape了。这是sql server的事情了。

 
 
 
 
 
 
评论列表

llnq SqlMethods like的更多相关文章

  1. LINQ to SQL语句(9)之Top/Bottom和Paging和SqlMethods

    适用场景:适量的取出自己想要的数据,不是全部取出,这样性能有所加强. Take 说明:获取集合的前n个元素:延迟.即只返回限定数量的结果集. var q = ( from e in db.Employ ...

  2. C#Linq中的Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods,skip,take,takewhile,skipwhile,编译查询等

    我们继续讲解LINQ to SQL语句,这篇我们来讨论Union All/Union/Intersect操作和Top/Bottom操作和Paging操作和SqlMethods操作 . Union Al ...

  3. ASP.NET MVC 解决LINQ表达式中的SqlMethods 未找到命名空间问题

    右键项目属性下的引用: 添加引用: 搜索寻找——System.Data.Linq,然后添加成功,即可解决LINQ表达式中的SqlMethods 未找到命名空间问题

  4. Linq无聊练习系列6--Any/All/Contains/Concat/Union/Intersect/Except/take/skip/SqlMethods操作练习

    /*********************Any/All/Contains/Concat/Union/Intersect/Except/take/skip/SqlMethods操作练习******* ...

  5. LINQ to SQL语句之Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods

    我们继续讲解LINQ to SQL语句,这篇我们来讨论Union All/Union/Intersect操作和Top/Bottom操作和Paging操作和SqlMethods操作 . Union Al ...

  6. Linq to SQL -- Union All、Union、Intersect和Top、Bottom和Paging和SqlMethods

    Union All/Union/Intersect操作 适用场景:对两个集合的处理,例如追加.合并.取相同项.相交项等等. Concat(连接) 说明:连接不同的集合,不会自动过滤相同项:延迟. 1. ...

  7. [转]C#Linq中的Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods,skip,take,takewhile,skipwhile,编译查询等

    本文转自:http://www.cnblogs.com/suizhikuo/p/3791799.html 我们继续讲解LINQ to SQL语句,这篇我们来讨论Union All/Union/Inte ...

  8. LINQ体验(8)——LINQ to SQL语句之Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods

    我们继续解说LINQ to SQL语句,这篇我们来讨论Union All/Union/Intersect操作和Top/Bottom操作和Paging操作和SqlMethods操作 . Union Al ...

  9. linq,sqlmethods,like

    LINQ to SQL will translate .NET methods in this manner: text.StartsWith(...) = LIKE ...% text.Contai ...

随机推荐

  1. [Sciter系列] MFC下的Sciter–3.Sciter脚本与底层交互

    [Sciter系列] MFC下的Sciter–3.Sciter脚本与底层交互,脚本调用底层自定义的方法函数. 本系列文章的目的就是一步步构建出一个功能可用,接口基本完善的基于MFC框架的SciterF ...

  2. HDU 4607 Park Visit (DP最长链)

    [题目]题意:N个城市形成一棵树,相邻城市之间的距离是1,问访问K个城市的最短路程是多少,共有M次询问(1 <= N, M <= 100000, 1 <= K <= N). [ ...

  3. 我的ECshop二次开发从零开始

    我是一个EC新手,EC就算做再多的模板,肯定也满足不了我们的需要,更何况各行有各行的门道,EC统一做出来的模板也不一定合适于我们这个行业用,因此,只有我们真正掌握了自己做模板,修改模板的功夫,才能真正 ...

  4. Python 3 加密简介

    导读 Python 3 的标准库中是没多少用来解决加密的,不过却有用于处理哈希的库.在这里我们会对其进行一个简单的介绍,但重点会放在两个第三方的软件包:PyCrypto 和 cryptography ...

  5. Listview异步加载之优化篇

    异步加载图片基本思想: 1.      先从内存缓存中获取图片显示(内存缓冲) 2.      获取不到的话从SD卡里获取(SD卡缓冲) 3.      都获取不到的话从网络下载图片并保存到SD卡同时 ...

  6. OutputFormat中OutputCommitter解析

    在hadoop中,由于一个Task可能由多个节点同时运行,当每个节点完成Task时,一个Task可能会出现多个结果,为了避免这种情况的出现,使用了OutPutCommitter.所以OutPutCom ...

  7. C# Code for Downloading Stock Symbols z

    http://www.jarloo.com/download-stock-symbols/ If your using C# you can easily get the XML data using ...

  8. 转载:mac系统XAMPP配置虚拟主机

    安装完xampp后,想添加一个virsualhost,一直报错.查了半天资料,都是乱说,后来看到了一篇国外的文章,终于弄出来了,整理一下. 第一步,配置本地hosts sudo vi /etc/hos ...

  9. linux 命令 之chomd

    chmod用于改变文件或目录的访问权限.用户用它控制文件或目录的访问权限.该命令有两种用法.一种是包含字母和操作符表达式的文字设定法:另一种是包含数字的数字设定法.  1. 文字设定法 语法:chmo ...

  10. 第二百七十一天 how can I 坚持

    每天的内容应该是这个样子,做了什么,收获了什么,有哪些东西感动了你. 就像昨天看了个电影<解救吾先生>,看完没点感觉或感受是不可能的,刘德华扮演的吾先生最终获救,不仅仅是靠运气,多少还是因 ...