Linq To SQL makes a developer’s life easy for performing various database operations. If the database design is stable, LinQ To SQL will do all the jobs for you for maintaining consistency between data relations. Simple data retrieval, insertion, deletion, update, etc can be done in a very easy way, in some cases just by calling some functions on the LinQ objects. There are a number of ways also provided for using the functionality of SQL operators/keywords. I will try to provide as many examples as possible on them from time to time. For today, My main concern is to make you familiar with the ‘Like’ keyword and construct an easy alternative of SQL query with Like and LinQ together.
‘Like’ is a popular SQL syntax operator, that is used widely for performing search operations on database tables. Thanks To LinQ To SQL, we can now get this functionality with help of a few readily given functions or even query syntax as well. They are as follows:
Check Keyword Existence Anywhere:
Suppose, Wan to match both sides(SQL Syntax: “%keyword%”), that means, If we have a keyword and want all results that contain this keyword and have anything prior/next to it, then, we can use the following function:
Similarly, for matching the beginning and endings are as follows:
//matching first part of the column
List<Users> users= (List<Users=>)TVDB.Users.Where(u => u.Username.StartsWith(keyword)).ToList();
//matching last part of the column
List<Users=> users= (List<Users>)TVDB.Users.Where(u => u.Username.EndsWith(keyword)).ToList();Code language:JavaScript(javascript)
If you like to use LinQ’s SQL-like statement syntax, sure you can use that too. Here is the examples of alternative SQL Like syntax:
//checks existence of a keywordList<Users> temp = (List<Users>)from u in TVDB.Users
where u.Username.Contains(keyword)
select u;
//check records that start with the keywordList<Users> temp = (List<Users>)from u in TVDB.Users
where u.Username.StartsWith(keyword)
select u;
//check record that end with the keywordList<Users> temp = (List<Users>)from u in TVDB.Users
where u.Username.EndsWith(keyword)
select u;
Code language:PHP(php)
Using LinQ To SQL Like As SQL Syntax:
Also, there is another more useful alternative way to achieve this, even more, handy that can be used to search with a regular expression pattern. That is provided by a .NET class, ‘SqlMethods’ (under ‘System.Data.Linq.SqlClient’ namespace). It has a method named ‘Like'(with 2 overloads) to which you can send parameters with keywords/patterns. Here is an example of using ‘SqlMethods.Like’ to achieve the same result as above:
List<Users> users = (List<Users>)from u in TVDB.Users
where SqlMethods.Like(u.Username,"%"+keyword+"%")
select u;
Code language:PHP(php)
They will generate SQL statements respectively as follows:
You can easily observe that ‘Contains(keyword)‘ method does the work of ‘%keyword%’, ‘StartsWith(keyword)’ does the task of ‘%’, and ‘EndsWith(keyword)‘ does the task of ‘%keyword’. On the other hand, with SQL Methods, you can use those symbols exactly like SQL queries.
Hope this small LinQ to SQL-like operator tutorial helps you understand the use of the ‘like’ operator along with LINQ queries more easily and efficiently. I will continuously try to provide more advanced tutorials on LinQ to SQL as much as possible. If you have any questions regarding this tutorial, feel free to ask them by commenting here. Happy coding 🙂
var JetpackInstantSearchOptions=JSON.parse(decodeURIComponent("%7B%22overlayOptions%22%3A%7B%22colorTheme%22%3A%22light%22%2C%22enableInfScroll%22%3Atrue%2C%22enableFilteringOpensOverlay%22%3Atrue%2C%22enablePostDate%22%3Atrue%2C%22enableSort%22%3Atrue%2C%22highlightColor%22%3A%22%23FFC%22%2C%22overlayTrigger%22%3A%22submit%22%2C%22resultFormat%22%3A%22expanded%22%2C%22showPoweredBy%22%3Atrue%2C%22defaultSort%22%3A%22relevance%22%2C%22excludedPostTypes%22%3A%5B%5D%7D%2C%22homeUrl%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%22%2C%22locale%22%3A%22en-US%22%2C%22postsPerPage%22%3A5%2C%22siteId%22%3A18994550%2C%22postTypes%22%3A%7B%22post%22%3A%7B%22singular_name%22%3A%22Post%22%2C%22name%22%3A%22Posts%22%7D%2C%22page%22%3A%7B%22singular_name%22%3A%22Page%22%2C%22name%22%3A%22Pages%22%7D%2C%22attachment%22%3A%7B%22singular_name%22%3A%22Media%22%2C%22name%22%3A%22Media%22%7D%7D%2C%22webpackPublicPath%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-content%5C%2Fplugins%5C%2Fjetpack%5C%2Fjetpack_vendor%5C%2Fautomattic%5C%2Fjetpack-search%5C%2Fbuild%5C%2Finstant-search%5C%2F%22%2C%22isPhotonEnabled%22%3Afalse%2C%22isFreePlan%22%3Atrue%2C%22apiRoot%22%3A%22https%3A%5C%2F%5C%2Fcodesamplez.com%5C%2Fwp-json%5C%2F%22%2C%22apiNonce%22%3A%22155bc22a78%22%2C%22isPrivateSite%22%3Afalse%2C%22isWpcom%22%3Afalse%2C%22hasOverlayWidgets%22%3Afalse%2C%22widgets%22%3A%5B%5D%2C%22widgetsOutsideOverlay%22%3A%5B%5D%2C%22hasNonSearchWidgets%22%3Afalse%2C%22preventTrackingCookiesReset%22%3Afalse%7D"));
codemake says
okey very good but I want to
my string “abc zxy 123” … n
SELECT * FROM Users WHERE
UserName LIKE ‘%abc%’ OR
Name LIKE ‘%zxy%’ OR Name ‘%123%’
… n
???
John G. says
Thank you! This is exactly what I was looking for. I am new to LINQ and find it great but just have to learn some of the ends and outs.
Luis Villarroel says
Thank friend