Click here to Skip to main content
15,889,651 members
Articles / Programming Languages / T-SQL

SqlMethod LIKE

Rate me:
Please Sign up or sign in to vote.
4.00/5 (2 votes)
15 Aug 2011CPOL 32.2K   1
In this post, I am going to discuss about the special method available in .NET Framework which allows to perform the like operation as we do in the T-SQL when searching data with string.

In this post, I am going to discuss about the special method available in .NET Framework which allows to perform the like operation as we do in the T-SQL when searching data with string.

In SQL to search string data query is:

SQL
--Searching string contains abc i.e. prabcfg, abcpr
Select * from table name where columnname like '%abc%'
--Searching string starts with abc i.e abcpr, abcrana
Select * from table name where columnname like 'abc%'
--Searching string ends with abc i.e prabc, ranaabc
Select * from table name where columnname like '%abc'
--Searching string with wildcard char _ i.e abec,abfc
Select * from table name where columnname like 'ab_c'

Now in LINQ, to achieve the same thing, we have functions like StartsWith, EndsWith and Contains. So LINQ query is:

C#
//Searching string contains abc i.e prabcfg, abcpr
var user = form u in users where u.Name.Contains("abc");
//Searching string starts with abc i.e abcpr, abcrana
var user = form u in users where u.Name.StartsWith("abc");
//Searching string ends with abc i.e prabc, ranaabc
var user = form u in users where u.Name.EndsWith("abc");

But with the LINQ, I am not able to achieve the last case(wildcard char _ ) and many more that I can do in like condition of the T-SQL.

SqlMethod class has a static method Like which allows to perform the same function as the like keyword of T-SQL. So the query with the LINQ is:

C#
var emplist = from emp in dbcontext.Employees
where SqlMethods.Like(emp.FirstName, "pr_nay")
select emp;

When you execute the statement, you can see the T-SQL statement same as above, i.e., wildcard statement listed above, you can view the query in the SQL profiler.
But the Like method works when you do the query using LINQ TO SQL only.

This article was originally posted at http://pranayamr.blogspot.com/2011/08/sqlmethod-like.html

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Microsoft C# MVP (12-13)



Hey, I am Pranay Rana, working as a Team Leadin MNC. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 5.5 years now.

For me def. of programming is : Programming is something that you do once and that get used by multiple for many years

You can visit my blog


StackOverFlow - http://stackoverflow.com/users/314488/pranay
My CV :- http://careers.stackoverflow.com/pranayamr

Awards:



Comments and Discussions

 
GeneralMy vote of 4 Pin
Everything Matters15-Aug-11 22:59
Everything Matters15-Aug-11 22:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.