日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

Linq to SQL Like Operator(转)

發(fā)布時間:2025/7/14 数据库 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Linq to SQL Like Operator(转) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
http://www.cnblogs.com/freeliver54/archive/2009/09/05/1560815.html


本文轉(zhuǎn)自: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!

總結(jié)

以上是生活随笔為你收集整理的Linq to SQL Like Operator(转)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。