llnq SqlMethods like

简介: http://www.cnblogs.com/freeliver54/archive/2009/09/05/1560815.html http://www.cnblogs.com/chen1388/archive/2010/03/12/1684450.

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的事情了。

 
 
 
 
 
 

目录
相关文章
|
3月前
|
人工智能 搜索推荐 数据挖掘
2025做SEO 要花多少钱?收费标准超清楚!
SEO起源于美国,2015年后在中国逐渐专业化。因Google技术成熟,外贸网站排名多被国外占据,新站需系统化投入才能超越。SEO费用无统一标准,受行业、预算、目标等多因素影响,月费通常500至1万美元。企业应视其为长期投资,合理规划预算,选择可靠服务商或工具入行。
|
机器学习/深度学习 自动驾驶 人机交互
深度学习之虚拟人类行为模拟
基于深度学习的虚拟人类行为模拟是指使用深度学习技术来模仿和预测虚拟环境中人类的行为,从而创建逼真的、智能化的虚拟角色。
299 4
|
SQL 分布式计算 DataWorks
DataWorks产品使用合集之如何获取表的分区列表
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
305 2
|
9月前
skynet.start 的作用详细解析
skynet.start 的作用详细解析
220 3
|
网络安全 开发工具 数据安全/隐私保护
git远程操控gitee
本文介绍了如何配置SSH公钥以实现Git的SSH远程登录Gitee,包括生成SSH密钥对、将公钥添加至Gitee账户、克隆仓库、同步本地更改至Gitee、删除远程文件以及查看和管理远程仓库的操作步骤。
273 5
|
Shell
esp32入门笔记
这篇文章是关于ESP32 S3入门的笔记,包括了安装编译工具、下载ESP-IDF框架、设置工具和环境变量、以及烧录固件的步骤说明。
423 5
|
人工智能
解决方案评测|通义万相AI绘画创作获奖名单
通义万相AI绘画创作获奖名单正式发布!
443 1
|
机器学习/深度学习 人工智能 自然语言处理
AI与人类协作的未来:探索智能辅助系统的新篇章
本文旨在探讨人工智能(AI)如何在未来的工作中与人类形成更紧密的合作关系。通过分析当前的技术趋势和未来的发展预测,我们将揭示AI如何成为增强人类能力的助手,而非替代者。文章将详细讨论AI在医疗、教育和创意产业中的应用案例,并展望未来AI技术的发展方向和潜在的社会影响。最后,我们将反思这种合作对人类社会的深远意义,并提出对未来工作场景的建议。
LabVIEW中不同颜色连线的含义
LabVIEW中不同颜色连线的含义
457 2
|
机器学习/深度学习 数据可视化 算法
探索数据科学中的模型可解释性
在数据科学的领域中,模型的可解释性已成为一个日益重要的议题。本文将深入探讨为什么模型可解释性对于数据科学家至关重要,以及如何通过特定的方法提高模型的解释能力。我们将从理论和实践两个角度出发,分析模型可解释性的重要性,并介绍几种提高模型可解释性的技术手段,如特征重要性评估、局部可解释性模型以及模型可视化技术等。文章旨在为读者提供一套实用的工具和方法,以增强其数据模型的透明度和可信度。