LINQ to SQL LIKE Operator

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

Join In Linq










GROUP JOIN As SubQuery




Comments

Popular posts from this blog

Sites.Selected | Graph API SharePoint Permission

Configure the SharePoint Online App Catalog

Azure Function | Sharepoint List item | Call from Power Automate Flow