Linq to SQL Like Operator(×ªÔØ)
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
Ïà¹ØÎĵµ£º
ϵͳ»·¾³£ºWindows 7
Èí¼þ»·¾³£ºVisual C++ 2008 SP1 +SQL Server 2005
±¾´ÎÄ¿µÄ£º±àдһ¸öº½¿Õ¹ÜÀíϵͳ
ÕâÊÇÊý¾Ý¿â¿Î³ÌÉè¼ÆµÄ³É¹û£¬ËäÈ»³É¼¨²»¼Ñ£¬µ«ÊÇ×÷ΪÎÒÓÃVC++ ÒÔÀ´±àдµÄ×î´ó³ÌÐò»¹ÊÇ´«µ½ÍøÉÏ£¬ÒÔ¹©²Î¿¼¡£ÓÃVC++ ×öÊý¾Ý¿âÉè¼Æ²¢²»ÈÝÒ×£¬µ«Ò²²»ÊDz»¿ÉÄÜ¡£ÒÔÏÂÊÇÎҵijÌÐò½çÃæ£¬ºóÃæ ......
×î½üÐèÒª²âÊÔÒ»¸öÏîÄ¿, ÐèÒªSQL SERVER 2008, ÔÚ΢ÈíÏÂÔØÆÀ¹À°æºó½øÐа²×°
Öм䷢ÏÖÁËÖÖÖÖ°²×°Ê§°ÜµÄÎÊÌ⣬¶Ô¸Ã¹ý³Ì½øÐÐÁ˼Ǽ£¬Ï£Íû¸øÓöµ½´ËÀàÎÊÌâµÄÅóÓÑÃǹ²Í¬Ì½ÌÖ£¬ÓÐËù°ïÖú£º
1. WINDOWS 2003, WINDOWS 7 ÐÂ×°µÄʱºò¿ÉÐУ¬µ«ÊÇÈç¹ûÓÐɾ³ýºóÖØÐ°²×°ÔòÎÞ·¨Ôٴΰ²×°
2. ¸ù¾ÝÍøÉÏÎÞÊýÏÈÇýµÄʧ°ÜÌáʾ£¬ ½øÐÐÁËÎÞÊýÖØÊÔ£¬ ......
Introduction
Microsoft SQL Server 2008 R2 is the latest release of SQL Server. This article will introduce the top 10 features and benefits of SQL Server 2008 R2. The “R2” tag indicates this is an intermediate release of SQL Server and not a major revision. However, there are a number o ......
Ò»¡¢±¸·ÝÊý¾Ý¿â
1¡¢´ò¿ªSQLÆóÒµ¹ÜÀíÆ÷£¬ÔÚ¿ØÖÆÌ¨¸ùĿ¼ÖÐÒÀ´Îµã¿ªMicrosoft SQL Server
2¡¢SQL Server×é-->Ë«»÷´ò¿ªÄãµÄ·þÎñÆ÷-->Ë«»÷´ò¿ªÊý¾Ý¿âĿ¼
3¡¢Ñ¡ÔñÄãµÄÊý¾Ý¿âÃû³Æ£¨ÈçÂÛ̳Êý¾Ý¿âForum£©-->È»ºóµãÉÏÃæ²Ëµ¥ÖеŤ¾ß-->Ñ¡Ôñ±¸·ÝÊý¾Ý¿â
4¡¢±¸·ÝÑ¡ÏîÑ¡ÔñÍêÈ«±¸·Ý£¬Ä¿µÄÖеı¸·Ýµ½Èç¹ûÔÀ´Óз¾¶ºÍÃû³ ......
¡¾AUTOTRACE¡¿SQLÓÅ»¯µÄÖØÒª¹¤¾ß--AUTOTRACE
Ìáµ½SQLÓÅ»¯£¬²»Äܲ»ÌáAUTOTRACEµÄÇ¿´ó¹¦ÄÜ¡£Ê¹ÓÃÆðÀ´·Ç³£±ã½Ý£¬²»¹ýÔÚÊÇʹÓÃ֮ǰ£¬ÐèÒª×öһЩÅäÖõŤ×÷¡£¼òÒªµÄÃèÊöÒ»ÏÂÕâ¸ö¹ý³Ì£¬¹©Ã»ÓÐʹÓùýµÄÅóÓѲο¼¡£
1.ʹÓÃsysÓû§Ö´ÐÐplustrce½Å±¾
sys@ora10g> @?/sqlplus/admin/plustrce
sys@ora10g> drop role plustrace ......