LINQ to SQL Select Query

In LINQ to SQL, the select query is useful to get all the records or rows from the table, and we can use LINQ to SQL select query to filter table records with where clause.

We can perform multiple operations like grouping, joining, etc., using LINQ to SQL select queries based on our requirements.

Syntax of LINQ to SQL Select Query

Following is the syntax of using LINQ to SQL Select query to get required records or rows from the table.

 

C# Code

 

EmployeeDBDataContext db = new EmployeeDBDataContext();
var result = from ed in db.EmployeeDetails
             select new
             {
               EmployeeName = ed.EmpName,
               Location = ed.Location
             };

VB.NET Code

 

Dim db As New EmployeeDBDataContext()
Dim result = From ed In db.EmployeeDetails Select New With {.EmployeeName = ed.EmpName, .Location = ed.Location, .Gender = ed.Gender}

If you observe the above syntax, we are getting records from the “EmployeeDetails” table using LINQ to SQL Select query.

Example of LINQ to SQL Select Query

Before we start implementing LINQ to SQL inner join example, 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 the .dbml file, we will show data in our application for that; Right-click on the application à select Add à New Item à Select Web Form à Give name as Default.aspx, and click OK button.

 

Now open the Default.aspx page and write the code 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 the code behind file and write the code as shown below.

 

C# Code

 

using System;
using System.Web.UI;
using System.Linq;

namespace LINQ2SQL
{
  public partial class Default : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!Page.IsPostBack)
      {
        EmployeeDBDataContext db = new EmployeeDBDataContext();
        var result = from ed in db.EmployeeDetails
                     select new
                     {
                       EmployeeName = 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
If Not Page.IsPostBack Then
Dim db As New EmployeeDBDataContext()
Dim result = From ed In db.EmployeeDetails Select New With {.EmployeeName = ed.EmpName, .Location = ed.Location, .Gender = ed.Gender}
gvDetails.DataSource = result
gvDetails.DataBind()
End If
End Sub
End Class

If you observe the above example, we are getting details from the “EmployeeDetails” table using LINQ to SQL Select query.

Result of LINQ to SQL Select Query Example

Following is the result of the LINQ to SQL stored procedure example.

 

LINQ to SQL Select Query Example Output

 

This is how we can use LINQ to SQL Select query in c#, vb.net to get multiple records or filter table data based on our requirements.