---
title: "LINQ to SQL LIKE operator with C#"  
description: "here you will learn about like operator in linq to sql that is Contains, StartsWith, EndsWith functions."  
author: "Ashutosh Patel"  
published: 2025-02-14  
updated: 2025-02-14  
canonical: https://www.mindstick.com/articles/338538/linq-to-sql-like-operator-with-c-sharp  
category: "linq"  
tags: ["linq", "linq to sql", "linq function"]  
reading_time: 5 minutes  

---

# LINQ to SQL LIKE operator with C#

In SQL, the **LIKE** operator is used for [pattern matching](https://www.mindstick.com/articles/1865/pattern-matching-in-erlang) with wildcards `(%, _)`. In LINQ to SQL, we use `Contains()`, `StartsWith()`, and `EndsWith()` to achieve the same functionality.

**Let's connect the SQL Sever database to our [Console Application](https://answers.mindstick.com/qa/49661/how-to-create-a-small-game-program-in-c-sharp-console-application)**

**[Entity Framework](https://www.mindstick.com/forum/12875/how-to-use-object-query-with-a-where-and-to-retrieve-entity-record-entity-framework) Core** is the modern and preferred way to use LINQ with SQL Server.

**Step 1: Install Required [NuGet Packages](https://answers.mindstick.com/qa/31181/some-nuget-packages-were-installed-using-a-target-framework-different-from-the-current-target-framework-and-may-need-to-be-reinstalled)**

install the below NuGet packages, Right click on your Project name from **Solution Explorer →** click on **Manage NuGet Packages..** → select **Browse** → search and install below all packages

```plaintext
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
```

Step 2: Add SQL [Server Database](https://www.mindstick.com/forum/155643/how-to-create-sql-server-database-in-google-cloud) to your Project

Click on **Server Explorer** → choose the **Connect to Database** option. A new popup will open like below image,

![LINQ to SQL LIKE operator with C#](https://www.mindstick.com/MindStickArticle/b634d08b-fabc-49d5-b8e3-6274dbad46d6/images/902ce52f-229e-432e-a12b-cf4fe731627e.png)

Click on **Test Connection** button, if connection is successful then click on **OK** button.

**Step 3: Create a [Database Context](https://www.mindstick.com/forum/12899/how-can-i-extend-an-api-controller-to-hold-a-variable-for-my-database-context)**

Now, create a **DbContext** class (`MyDbContext.cs`) to manage the [database connection](https://answers.mindstick.com/qa/93693/what-is-connection-string-in-database-connection) and communicate with the database.

```cs
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using MyConsoleApplication.Models;
namespace MyConsoleApplication
{
   class MyDbContext: DbContext
   {
       public DbSet<Employees> Employees { get; set; }
       protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
       {
           optionsBuilder.UseSqlServer("Server=YOUR_SERVER_NAME; Database=DATABASE_NAME; User ID=USER_ID;Password= YOUR_PASSWORD;");
       }
       protected override void OnModelCreating(ModelBuilder modelBuilder)
       {
           modelBuilder.Entity<Employees>()
                       .HasKey(e => e.EmpId);  // Define the primary key
       }
   }
}
```

**Note:** Replace `YOUR_SERVER_NAME`**,** `YourDatabase`**,** `User ID`**,** and `Password`with actual values in the [connection string](https://www.mindstick.com/forum/160447/how-to-configure-connection-string-in-dot-net-core-6).

Here the database connection will be established successfully.

**Step 4: Define a Model Class** `Employees.cs`

```cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyConsoleApplication.Models
{
   class Employees
   {
       [Key]
       public int EmpId { get; set; }
       public string EmpName { get; set; }
       public string Gender { get; set; }
       public DateTime CreationDate { get; set; }
       public Nullable<DateTime> ModificationDate { get; set; }
       public decimal Salary { get; set; }
       public int DepartmentId { get; set; }
   }
}
```

## Now, Perform the different LIKE operators in LINQ to SQL

Create a custom class in [your application](https://answers.mindstick.com/qa/97584/how-do-you-choose-the-correct-camera-for-your-application) to perform the different LIKE operators,

Let's see the result of `Employees` table

```cs
using System;
using System.Collections.Generic;
using System.Linq;
using MyConsoleApplication.Models;
namespace MyConsoleApplication
{
   class MindStickSoft
   {
       static void Main()
       {
           using (var db = new MyCollegeDbContext())
           {
               var employee = db.Employees.ToList();
               foreach (var Emp in employee)
               {
                   Console.WriteLine("Name: {0}  \t Gender: {1} \t Salary: {2}", Emp.EmpName, Emp.Gender, Emp.Salary);
               }
           }
       }
   }
}
```

## Output:

```plaintext
Name: Ashu       Gender: Male    Salary: 12410.0000
Name: Priya Shukla       Gender: Female          Salary: 70788.0000
Name: Ashutosh Verma     Gender: Male    Salary: 50066.0000
Name: Rani Sharma        Gender: Female          Salary: 57722.0000
Name: Amit Tiwari        Gender: Male    Salary: 81391.0000
Name: Ashu Patel         Gender: Male    Salary: 57659.0000
Name: Samiksha Mishra    Gender: Female          Salary: 15000.0000
Name: Akanksha Singh     Gender: Female          Salary: 15000.0000
Name: Shinu      Gender: Male    Salary: 21450.0000
Name: Tejasvi Raj        Gender: Male    Salary: 32145.0000
```

**Using** `Contains()` **(Equivalent to** `LIKE '%value%'`**)**

```cs
using (var db = new MyCollegeDbContext())
           {
               var employee = db.Employees.Where(x => x.EmpName.Contains("As")).ToList();
               foreach (var Emp in employee)
               {
                   Console.WriteLine("Name: {0}  \t Gender: {1} \t Salary: {2}", Emp.EmpName, Emp.Gender, Emp.Salary);
               }
           }
```

## Output:

```plaintext
Name: Ashu       Gender: Male    Salary: 12410.0000
Name: Ashutosh Verma     Gender: Male    Salary: 50066.0000
Name: Ashu Patel         Gender: Male    Salary: 57659.0000
Name: Tejasvi Raj        Gender: Male    Salary: 32145.0000
```

**Using** `StartsWith()` **(Equivalent to** `LIKE 'value%'`**)**

```cs
using (var db = new MyCollegeDbContext())
           {
               var employee = db.Employees.Where(x => x.EmpName.StartsWith("As")).ToList();
               foreach (var Emp in employee)
               {
                   Console.WriteLine("Name: {0}  \t Gender: {1} \t Salary: {2}", Emp.EmpName, Emp.Gender, Emp.Salary);
               }
           }
```

## Output:

```plaintext
Name: Ashu       Gender: Male    Salary: 12410.0000
Name: Ashutosh Verma     Gender: Male    Salary: 50066.0000
Name: Ashu Patel         Gender: Male    Salary: 57659.0000
```

**Using** `EndsWith()` **(Equivalent to** `LIKE '%value'`**)**

```cs
using (var db = new MyCollegeDbContext())
           {
               var employee = db.Employees.Where(x => x.EmpName.EndsWith("a")).ToList();
               foreach (var Emp in employee)
               {
                   Console.WriteLine("Name: {0}  \t Gender: {1} \t Salary: {2}", Emp.EmpName, Emp.Gender, Emp.Salary);
               }
           }
```

## Output:

```plaintext
Name: Priya Shukla       Gender: Female          Salary: 70788.0000
Name: Ashutosh Verma     Gender: Male    Salary: 50066.0000
Name: Rani Sharma        Gender: Female          Salary: 57722.0000
Name: Samiksha Mishra    Gender: Female          Salary: 15000.0000
```

**Using Different** `LIKE` **Queries in One Code**

Here, you will perform multiple like operators simultaneously.

```cs
using (var db = new MyCollegeDbContext())
           {
               var employee = db.Employees.Where(x => x.EmpName.StartsWith("As") || x.EmpName.Contains("As") || x.EmpName.EndsWith("a")).ToList();
               foreach (var Emp in employee)
               {
                   Console.WriteLine("Name: {0}  \t Gender: {1} \t Salary: {2}", Emp.EmpName, Emp.Gender, Emp.Salary);
               }
           }
```

## Output:

```plaintext
Name: Ashu       Gender: Male    Salary: 12410.0000
Name: Priya Shukla       Gender: Female          Salary: 70788.0000
Name: Ashutosh Verma     Gender: Male    Salary: 50066.0000
Name: Rani Sharma        Gender: Female          Salary: 57722.0000
Name: Ashu Patel         Gender: Male    Salary: 57659.0000
Name: Samiksha Mishra    Gender: Female          Salary: 15000.0000
Name: Tejasvi Raj        Gender: Male    Salary: 32145.0000
```

**Best practice:** Use `Contains()`, `StartsWith()`, and `EndsWith()` for pattern matching.

Hope you understand clearly.

Thanks.

**Also, read:** [How to use LINQ to SQL Select Query using C#](https://www.mindstick.com/articles/338528/how-to-use-linq-to-sql-select-query-using-c-sharp)

---

Original Source: https://www.mindstick.com/articles/338538/linq-to-sql-like-operator-with-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
