LINQ to SQL Like (Contains, Startwith, Endswith)

  Total Views : 128
  Comments : 0
 
  Keywords : Linq to sql like operator with example in c# / vb.net, How to use linq to sql like operator to get matching elements from collections in c# / vb.net with example

Here we will learn LINQ to SQL like operator in c#, vb.net with example and LINQ to SQL contains(), startwith() and endswith() methods in c#, vb.net with example.

LINQ to SQL Like Operator

In LINQ to SQL we don't have LIKE operator but by using contains(), startswith() and endswith() methods we can implement LIKE operator functionality in LINQ to SQL.

 

Following table shows more details regarding operators which we used to achieve LINQ to SQL Like operators.

 

OperatorDescription
Contains() It is equivalent to '%string%'
StartsWith() It is equivalent to 'string%'
EndsWith() It is equivalent to '%string'

Before we start implement LINQ to SQL LIKE operator examples first we need to create a database with required tables and map those tables to LINQ to SQL file (.dbml) if you don't know the process don't worry check this link create and map database tables to LINQ to SQL file (.dbml).

 

Once we create and map required tables to .dbml file now we will show data in our application for that Right click on application → select Add → New Item → Select Web Form → Give name as Default.aspx and click OK button.

 

Now open Default.aspx page and write the code like as shown below

 

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Show Employee Details in Gridview</title>

</head>

<body>

<form id="form1" runat="server">

<div>

<asp:GridView ID="gvDetails" runat="server"></asp:GridView>

</div>

</form>

</body>

</html>

Now open code behind file and write the code like as shown below

LINQ to SQL Contains() Operator

In LINQ to SQL by using Contains() operator we can get records or rows which contains defined text.

 

C# Code

 

protected void Page_Load(object sender, EventArgs e)

{

EmployeeDBDataContext db = new EmployeeDBDataContext();

if (!Page.IsPostBack)

{

var result = from ed in db.EmployeeDetails where ed.EmpName.Contains("su")

select new

{

Name = ed.EmpName,

Location = ed.Location,

Gender = ed.Gender

};

gvDetails.DataSource = result;

gvDetails.DataBind();

}

}

VB.NET Code

 

Public Class WebForm1

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim db As New EmployeeDBDataContext()

If Not Page.IsPostBack Then

Dim result = From ed In db.EmployeeDetails Where ed.EmpName.Contains("su") Select New With {.Name = ed.EmpName, .Location = ed.Location, .Gender = ed.Gender}

gvDetails.DataSource = result

gvDetails.DataBind()

End If

End Sub

End Class

If you observe above example, we are trying to get records from “EmployeeDetails” table where it contains “su” word.

Output of LINQ to SQL Contains Example

Following is the result of LINQ to SQL Contains operator example.

 

LINQ to SQL Contains Example Result

LINQ to SQL StartsWith() Operator

In LINQ to SQL StartsWith() operator is used to get records or rows from table where records starts with the defined word.

 

C# Code

 

protected void Page_Load(object sender, EventArgs e)

{

EmployeeDBDataContext db = new EmployeeDBDataContext();

if (!Page.IsPostBack)

{

var result = from ed in db.EmployeeDetails where ed.EmpName.StartsWith("s")

select new

{

Name = ed.EmpName,

Location = ed.Location,

Gender = ed.Gender

};

gvDetails.DataSource = result;

gvDetails.DataBind();

}

}

VB.NET Code

 

Public Class WebForm1

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim db As New EmployeeDBDataContext()

If Not Page.IsPostBack Then

Dim result = From ed In db.EmployeeDetails Where ed.EmpName.StartsWith("s") Select New With {.Name = ed.EmpName, .Location = ed.Location, .Gender = ed.Gender}

gvDetails.DataSource = result

gvDetails.DataBind()

End If

End Sub

End Class

If you observe example, we are getting records from "EmployeeDetails" table where name starts with "s".

Output of LINQ to SQL StartsWith() Example

Following is the result of LINQ to SQL StartsWith() example.

 

LINQ to SQL Startswith Example in C#, VB.NET

LINQ to SQL EndsWith() Operator

In LINQ to SQL EndsWith(() operator is used to return records or rows from table where records ends with defined word.

 

C# Code

 

protected void Page_Load(object sender, EventArgs e)

{

EmployeeDBDataContext db = new EmployeeDBDataContext();

if (!Page.IsPostBack)

{

var result = from ed in db.EmployeeDetails where ed.EmpName.EndsWith("la")

select new

{

Name = ed.EmpName,

Location = ed.Location,

Gender = ed.Gender

};

gvDetails.DataSource = result;

gvDetails.DataBind();

}

}

VB.NET Code

 

Public Class WebForm1

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim db As New EmployeeDBDataContext()

If Not Page.IsPostBack Then

Dim result = From ed In db.EmployeeDetails Where ed.EmpName.EndsWith("la") Select New With {.Name = ed.EmpName, .Location = ed.Location, .Gender = ed.Gender}

gvDetails.DataSource = result

gvDetails.DataBind()

End If

End Sub

End Class

If you observe above example, we are getting records from “EmployeeDetails” table where name ends with “la

Output LINQ EndsWith() Example

Following is the result of LINQ EndsWith() example.

 

LINQ to SQL Endswith Operator in C#, VB.NET Example Result

 

This is how we can achieve Like functionality in LINQ to SQL using Contains(), Startswith() and Endswith() operations in c#, vb.net.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User