http://www.cnblogs.com/freeliver54/archive/2009/09/05/1560815.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%]

Enjoy!

Linq to SQL Like Operator(转)相关推荐

  1. ScottGu之博客翻译-LINQ to SQL第三部分,查询数据库 (Part 3 - Querying our Database)

     本贴只为共享知识,更为简洁(即无英文的版本)将会发布在博客堂上,堂主正对此文进行审阅. 希望本贴能对您的LINQ to SQL语言的学习有一定的帮助! 原贴链接: http://weblogs.as ...

  2. 如何查看Linq to SQL运行时,实际执行的Sql语句

    调试Linq to sql代码是, 如果遇到错误,很难判断错误的原因是什么,如果能够输出实际执行的sql原文,对于我们寻找错误的原因有有很大帮助. 以下是我用到的方法: StringBuilder s ...

  3. Linq to SQL 资源

    Scott Guthrie 的 Linq to SQL 系列: 1)介绍 http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to ...

  4. .NET 3.5(12) - DLINQ(LINQ to SQL)之事务处理和并发处理

    步步为营VS 2008 + .NET 3.5(12) - DLINQ(LINQ to SQL)之事务处理和并发处理 作者:webabcd 介绍 以Northwind为示例数据库,DLINQ(LINQ ...

  5. 一步一步学Linq to sql(六):探究特性

      延迟执行 IQueryable query = from c in ctx.Customers select c; 这样的查询句法不会导致语句立即执行,它仅仅是一个描述,对应一个SQL.仅仅在需要 ...

  6. LINQ to SQL 在 Visual Studio 2008 中的简单应用

    在.Net Framework 3.5 中,最激动人心的就是增加了LINQ功能,LINQ在数据集成的基础上提供了新的轻型方式.有了LINQ,我们创建的查询现在就编程了.Net 框架的一个成员,在对要操 ...

  7. 一步一步学Linq to sql(一):预备知识

    从今天起将推出新手讲堂,首先从linq开始详细讲解.一步一步学Linq to sql(一):预备知识 什么是Linq to sql Linq to sql(或者叫DLINQ)是LINQ(.NET语言集 ...

  8. 一步一步学linq to sql(四)查询句法

    select 描述:查询顾客的公司名.地址信息 查询句法: var 构建匿名类型1 = from c in ctx.Customers select new { 公司名 = c.CompanyName ...

  9. 在.NET 3.5 平台上使用LINQ to SQL创建三层/多层Web应用系统 (Part 1)

    在.NET 3.5 平台上使用LINQ to SQL创建三层/多层Web应用系统 (Part 1) 在新的.Net Framework 3.5平台上,Microsoft发布了LINQ(C# 3.0, ...

最新文章

  1. 创业丨中国人工智能领域投资机构10强榜单
  2. 《程序员代码面试指南》第二章 链表问题 构造链表和节点的实体
  3. JavaScript的作用域与闭包
  4. 全志 移除屏幕超时选项 Patch
  5. 【java学习】Arraylist和LinkedList使用场景与性能对比
  6. Java代理初学者指南
  7. 定位pure virtual method called问题
  8. 2015.09.05 C++中类的static与const成员
  9. 线程相关函数(1)-pthread_create(), pthread_join(), pthread_exit(), pthread_cancel() 创建取消线程...
  10. 传海思为 PC 开发 CPU/GPU ;小米将发布第二款 5G 手机;Firefox 68.0.2 发布 | 极客头条...
  11. Flex样式工作原理
  12. 【java笔记】Object类
  13. Git 时光穿梭鸡 管理修改
  14. 如何着手分析一个行业?
  15. 7.业务架构·应用架构·数据架构实战 --- 业务架构书
  16. css居中的几种方法_css两种常用的不定宽高的水平垂直居中方法,记住它,不再为样式发愁...
  17. 写论文时introduction and realted works部分如何写别人的工作,论文写作常用词
  18. Python SQLite3 教程
  19. python基本类型关键字_python基本类型关键字_python中的关键字---1(基础数据类)...
  20. 小知识:btn.addEventListener is not a function报错处理

热门文章

  1. hdu 2842 Chinese Rings 矩阵快速幂
  2. iptables防火墙的连接状态
  3. java美元兑换,(Java实现) 美元汇率
  4. jupyterlab debugger+显示图片
  5. java基础-容器-Set
  6. 博弈论经典算法(一)——对抗搜索与Alpha-Beta剪枝
  7. Tomcat工作原理
  8. 编程之美 --1 : 骨牌覆盖问题·一
  9. linux stat函数讲解
  10. gearman简介及安装使用