---
title: "How to use LINQ to SQL Select Query using C#"  
description: "here you will learn about how to use linq to sql with new connection establish from sql server database and perform various linq to sql functions."  
author: "Ashutosh Patel"  
published: 2025-02-13  
updated: 2025-02-13  
canonical: https://www.mindstick.com/articles/338528/how-to-use-linq-to-sql-select-query-using-c-sharp  
category: "linq"  
tags: ["linq", "linq to sql", "linq function"]  
reading_time: 3 minutes  

---

# How to use LINQ to SQL Select Query using C#

### What is LINQ to SQL?

LINQ to SQL allows querying [relational databases](https://answers.mindstick.com/qa/102683/how-does-google-s-cloud-sql-manage-relational-databases-in-the-cloud) (like [SQL Server](https://www.mindstick.com/articles/12620/use-of-in-operator-in-sql-server)) using **C# [LINQ queries](https://www.mindstick.com/forum/159878/what-is-the-role-of-the-join-keyword-in-linq-queries)** instead of raw SQL. It maps database tables to **C# classes** using an **Object Relational Mapper (ORM)**.

#### How to Establish a Connection to SQL Server for LINQ Queries in C#?

**[Entity Framework](https://www.mindstick.com/forum/12875/how-to-use-object-query-with-a-where-and-to-retrieve-entity-record-entity-framework) Core** is the modern and preferred way to use LINQ with SQL Server.

#### Step 1: Install Required NuGet Packages

install the below [NuGet packages](https://answers.mindstick.com/qa/31181/some-nuget-packages-were-installed-using-a-target-framework-different-from-the-current-target-framework-and-may-need-to-be-reinstalled), Right click on [your Project](https://answers.mindstick.com/qa/93843/is-it-necessary-to-add-bootstrap-js-and-bootstrap-css-both-in-your-project) name from **Solution Explorer →** click on **Manage NuGet Packages..** → select **Browse** → search and install below all packages

- Microsoft.EntityFrameworkCore
- Microsoft.EntityFrameworkCore.SqlServer
- Microsoft.EntityFrameworkCore.Tools

#### Step 2: Add SQL Server Database to your Project

Click on **Server Explorer** → choose the **Connect to Database** option. A new popup will open like below image,

![How to use LINQ to SQL Select Query using C#](https://www.mindstick.com/mindstickarticle/b634d08b-fabc-49d5-b8e3-6274dbad46d6/images/902ce52f-229e-432e-a12b-cf4fe731627e.png)

if **Test Connection** success then click on **OK.**

#### Step 3: Create a Database Context

Create a **DbContext** class to manage the [database connection](https://answers.mindstick.com/qa/93693/what-is-connection-string-in-database-connection).

```cs
using System;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using MyConsoleApplication.Models;
namespace MyConsoleApplication
{
   class MyDbContext: DbContext
   {
       public DbSet<Employees> Employees { get; set; }
       protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
       {
           optionsBuilder.UseSqlServer("Server=YOUR_SERVER_NAME; Database=DATABASE_NAME; User ID=USER_ID;Password= YOUR_PASSWORD;");
       }
       protected override void OnModelCreating(ModelBuilder modelBuilder)
       {
           modelBuilder.Entity<Employees>()
                       .HasKey(e => e.EmpId);  // Define the primary key
       }
   }
}
```

Replace **YOUR_SERVER_NAME, YourDatabase, User ID,** and **Password** with actual values.

#### Step 4: Define a Model Class `Employees.cs`

```cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MyConsoleApplication.Models
{
   class Employees
   {
       [Key]
       public int EmpId { get; set; }
       public string EmpName { get; set; }
       public string Gender { get; set; }
       public DateTime CreationDate { get; set; }
       public Nullable<DateTime> ModificationDate { get; set; }
       public decimal Salary { get; set; }
       public int DepartmentId { get; set; }
   }
}
```

#### Step 5: Perform LINQ Queries

```cs
using System;
using System.Collections.Generic;
using System.Linq;
using MyConsoleApplication.Models;
namespace MyConsoleApplication
{
   class MindStickSoft
   {
       static void Main()
       {
           using (var db = new MyDbContext())
           {
               var employee = from emp in db.Employees
                              select emp;
               foreach (var Emp in employee)
               {
                   Console.WriteLine("Name: {0} Gender: {1}", Emp.EmpName, Emp.Gender);
               }
           }
       }
   }
}
```

## Output

```plaintext
Name: Ashu Gender: Male
Name: Priya Shukla Gender: Female
Name: Ashutosh Verma Gender: Male
Name: Rani Sharma Gender: Female
Name: Amit Tiwari Gender: Male
Name: Ashu Patel Gender: Male
Name: Samiksha Mishra Gender: Female
Name: Akanksha Singh Gender: Female
Name: Shinu Gender: Male
Name: Tejasvi Raj Gender: Male
```

Your can perform more LINQ to [SQL select](https://www.mindstick.com/forum/157762/how-can-i-apply-if-then-in-an-sql-select) function like given below,

**Using** `Where` **with** `Select`

```plaintext
 var employee = from emp in db.Employees
                   where emp.Salary > 10000
                   select emp;
         foreach (var Emp in employee)
          {
             Console.WriteLine("Name: {0} Gender: {1}", Emp.EmpName, Emp.Gender);
          }
```

**Using** `Select` **with** `OrderBy`

```plaintext
var employee = from emp in db.Employees
                           orderby emp.EmpName
                           select emp;
           foreach (var Emp in employee)
           {
               Console.WriteLine("Name: {0} Gender: {1}", Emp.EmpName, Emp.Gender);
           }
```

and so on….

I hope you understand clearly.

Thanks

**Also, Read:** [Connected vs Disconnected Architecture in ADO.NET](https://www.mindstick.com/articles/338512/connected-vs-disconnected-architecture-in-ado-dot-net)

---

Original Source: https://www.mindstick.com/articles/338528/how-to-use-linq-to-sql-select-query-using-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
