• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Featured
    • C# Tutorials
      • LinQ Tutorials
      • Facebook C# API Tutorials
    • PHP Tutorials
      • CodeIgniter Tutorials
    • Amazon AWS Tutorials
  • Categories
    • Programming
    • Development
    • Database
    • Web Server
    • Source Control
    • Management
    • Project
  • About
  • Write
  • Contact

CodeSamplez.com

Programming, Web development, Cloud Technologies

You are here: Home / Database / LinQ Query With Like Operator

LinQ Query With Like Operator

March 15, 2011 by Rana Ahsan 5 Comments

linq to sql tutorials

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:

UsersDataContext myDB = new MyDataContext(); List<Users> users= (List<Users>)myDB .TVUsers.Where(u => u.Username.Contains(keyword)).ToList();
Code language: PHP (php)


This generates the SQL Query Like as follows:

SELECT * FROM Users WHERE Username Like [%keyword%]
Code language: CSS (css)

Read All LinQ Tutorials By CodeSamplez.com

Check Keyword Existence At the Beginning/End:

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 keyword List<Users> temp = (List<Users>)from u in TVDB.Users where u.Username.Contains(keyword) select u; //check records that start with the keyword List<Users> temp = (List<Users>)from u in TVDB.Users where u.Username.StartsWith(keyword) select u; //check record that end with the keyword List<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:

SELECT * FROM Users WHERE Username Like [%keyword%] SELECT * FROM Users WHERE Username Like [keyword%] SELECT * FROM Users WHERE Username Like [%keyword]
Code language: CSS (css)

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 🙂

Share If Liked

  • Click to share on Facebook (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pocket (Opens in new window)

You may also like

Filed Under: Database Tagged With: c#, linq

About Rana Ahsan

Rana is a passionate software engineer/Technology Enthusiast.
Github: ranacseruet

Reader Interactions

Comments

  1. codemake says

    June 30, 2012 at 1:53 pm

    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

    ???

    Reply
  2. John G. says

    December 15, 2012 at 7:44 am

    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.

    Reply
  3. Luis Villarroel says

    January 28, 2019 at 10:15 am

    Thank friend

    Reply

Trackbacks

  1. Linq Like? Best 30 Answer - Ar.taphoamini.com says:
    July 26, 2022 at 3:04 pm

    […] + Read More Here […]

    Reply
  2. Linq Like Query? The 20 Correct Answer - Ar.taphoamini.com says:
    July 26, 2022 at 4:04 pm

    […] + Read More Here […]

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Follow Us

  • Twitter
  • Facebook

Top Posts & Pages

  • How To Work With JSON In Node.js / JavaScript
    How To Work With JSON In Node.js / JavaScript
  • PHP HTML5 Video Streaming Tutorial
    PHP HTML5 Video Streaming Tutorial
  • LinQ Query With Like Operator
    LinQ Query With Like Operator
  • How To Work With C# Serial Port Communication
    How To Work With C# Serial Port Communication
  • Getting Started With Smarty Template Engine
    Getting Started With Smarty Template Engine
  • Generate HTTP Requests using c#
    Generate HTTP Requests using c#
  • Using Supervisord Web Interface And Plugin
    Using Supervisord Web Interface And Plugin
  • Facebook C# API Tutorials
    Facebook C# API Tutorials
  • Utilizing Config File In C#.NET Application
    Utilizing Config File In C#.NET Application
  • How To Use Hotkeys/Keyboard Events In WPF Application Using C#
    How To Use Hotkeys/Keyboard Events In WPF Application Using C#

Recent Posts

  • Building Auth With JWT – Part 2
  • Building Auth With JWT – Part 1
  • Document Your REST API Like A Pro
  • Understanding Golang Error Handling
  • Web Application Case Studies You Must Read

Tags

.net angularjs apache api audio auth authenticatin aws c# cloud server codeigniter deployment docker doctrine facebook git github golang htaccess html5 http javascript jwt linq mysql nodejs oop performance php phpmyadmin plugin process python regular expression scalability server smarty socket.io tfs tips unit-test utility web application wordpress wpf

Footer

Archives

Follow Us

  • Twitter
  • Facebook

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 3,774 other subscribers

Copyright © 2023