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. codevs 1172 Hankson 的趣味题

    woc....这题考细节处理.要注意代码的逻辑顺序还有不要作死地循环到sqrt+1. #include<iostream> #include<cstdio> #include& ...

  2. Oracle 手工清除回滚段的几种方法

    关于回滚段的问题,之前也小整理过一个,参考: Current online Redo 和 Undo 损坏的处理方法 http://blog.csdn.net/tianlesoftware/articl ...

  3. 计算时间间隔的js

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  4. C#中的lock关键字

    前几天与同事激烈讨论了一下,有一点收获,记录起来. 首先给出MSDN的定义: lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.这是通过在代码块运行期间为给定对象获取互斥锁来实现的. ...

  5. c# Chart设置样式

    一.chart属性熟悉 能够对C# chart控件属性熟悉起帮助的code, 现在分享给大家,Chart Operate Demo Code Download,如有帮助,别忘点个赞. Chart    ...

  6. 《C++ primer》--第9章

    习题9.2 创建和初始化一个vector对象有4种方式,为每种方式提供一个例子. 解答: 分配指定数目的元素,并对这些元素进行值初始化: vector<int> ivec(10);     ...

  7. acdream 1044

    题意:有你一个草坪,草的初始高度都是100,让你用割草机割,割草机只能横着或竖着割,每次割的高度一定,问你能不能割出给定的草坪出来. 考虑任意一个草被割要么是横着要么竖着,所以任意一个草必然是它所在行 ...

  8. [Irving] SQL 2005/SQL 2008 备份数据库并自动删除N天前备份的脚本

    以下为SQL脚本,本人以执行计划来调用,所以改成了执行命令,大家可根据自己需要改为存储过程使用 )='E:\MsBackUp\SqlAutoBackup\' --备份路径; --备份类型为全备,1为差 ...

  9. 常见设计模式解析和实现(C++)Prototype模式(原型模式)

    作用:用原型实例指定创建对象的种类,并且通过拷贝这些原型创建新的对象. UML结构图: 抽象基类: 1)  Prototype:虚拟基类,所有原型的基类,提供Clone接口函数 接口函数: 1)  P ...

  10. 建立简单的VLAN通信

    http://minitoo.blog.51cto.com/4201040/786011(转载) 在路由器上做单臂路由实现VLAN间路由,也就是设置子接口和封装协议. 实现环境如下图: 在交换机上建立 ...