LINQ to SQL LIKE Operator

By using Contains(), StartsWith(), EndsWith() we can implement LIKE operator in LINQ to SQL.

  • like '%SearchString%' = Contains("SearchString")
  • like '%SearchString' = StartsWith("SearchString")
  • like 'SearchString%' = EndsWith("SearchString")

Input tables

Employees table
employees


LINQ Query Contains():

MyDBDataContext sqlObj = new MyDBDataContext(); var employees = from emps in sqlObj.tblEmployees where emps.EmployeeName.Contains("en") select new { emps.EmployeeID, emps.EmployeeName, emps.Salary }; gvemployees.DataSource = employees; gvemployees.DataBind();

Output:

like-contains

LINQ Query StartsWith():

MyDBDataContext sqlObj = new MyDBDataContext(); var employees = from emps in sqlObj.tblEmployees where emps.EmployeeName.StartsWith("v") select new { emps.EmployeeID, emps.EmployeeName, emps.Salary }; gvemployees.DataSource = employees; gvemployees.DataBind();

Output:

like-startswith

LINQ Query EndsWith():

MyDBDataContext sqlObj = new MyDBDataContext(); var employees = from emps in sqlObj.tblEmployees where emps.EmployeeName.EndsWith("ms") select new { emps.EmployeeID, emps.EmployeeName, emps.Salary }; gvemployees.DataSource = employees; gvemployees.DataBind();

Output:

like-endswith