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.
Operator | Description |
---|---|
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
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.
Following is the result of LINQ to SQL Contains operator example.
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".
Following is the result of LINQ to SQL StartsWith() example.
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”
Following is the result of LINQ EndsWith() example.
This is how we can achieve Like functionality in LINQ to SQL using Contains(), Startswith() and Endswith() operations in c#, vb.net.