LINQ to SQL Select Query

  Total Views : 136
  Comments : 0
 
  Keywords : Linq to sql select query with example in c# / vb.net, How to use linq to sql select query to get all the rows from the table in asp.net web application using c# / vb.net with example

Here we will learn LINQ to SQL Select query in c#, vb.net with example and how to use LINQ to SQL Select query to get multiple records or rows or columns from table in c#, vb.net with examples.

LINQ to SQL Select Query

In LINQ to SQL select query is used to get all the records or rows from 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 query 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 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 above syntax we are getting records from “EmployeeDetails” table using LINQ to SQL Select query.

Example of LINQ to SQL Select Query

Before we start implement LINQ to SQL inner join example 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

 

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 above example, we are getting details from “EmployeeDetails” table using LINQ to SQL Select query.

Output of LINQ to SQL Select Query Example

Following is the result of 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.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User