---
title: "How to find the first and last data of SQL using LINQ?"  
description: "How to find the first and last data of SQL using LINQ?"  
author: "Ethan Karla"  
published: 2021-09-24  
updated: 2021-09-24  
canonical: https://www.mindstick.com/forum/156763/how-to-find-the-first-and-last-data-of-sql-using-linq  
category: "linq"  
tags: ["c#", "ado.net", "linq"]  
reading_time: 2 minutes  

---

# How to find the first and last data of SQL using LINQ?

How to find the first and last [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) of [SQL Database](https://www.mindstick.com/articles/337535/connecting-to-sql-databases-ado-dot-net-essentials) using [LINQ](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries)?

## Replies

### Reply by Ravi Vishwakarma

We use the First and Last methods in LINQ for finding the first element and last element.

1. **First()** are used to fetching the first row data from [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) and we can use the **FirstOrDefault()** method
2. **Last()** are used to fetching the last row data from SQL and we can use the **LastOrDefault()** method

```
using System;using System.Collections.Generic;using System.Linq;
public class Program{ public static void Main() {
  List<Student> students = new List<Student>()  {  new Student() { StudentId = 1, Name = 'Ashu', Marks = 500 },  new Student() { StudentId = 2, Name = 'Shyam', Marks = 300 },  new Student() { StudentId = 3, Name = 'Shriyam', Marks = 400 },  new Student() { StudentId = 4, Name = 'Sunny', Marks = 550 },  new Student() { StudentId = 5, Name = 'Ram', Marks = 600 },  new Student() { StudentId = 6, Name = 'Krishna', Marks = 700 },  new Student() { StudentId = 7, Name = 'Anupam', Marks = 550 }  };
  List<Student> list = students.Where( stu => stu.Marks > 500).ToList();
  Console.WriteLine('\nAll value :\n');  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');  list.ForEach( student => Console.WriteLine('{0,-10} {1,10} {2,5}', student.Name, student.StudentId, student.Marks) );
  Student first = students.Where( stu => stu.Marks > 500).First();
  Console.WriteLine('\nFirst() :\n');  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');  Console.WriteLine('{0,-10} {1,10} {2,5}', first.Name, first.StudentId, first.Marks);
  Student last = students.Where( stu => stu.Marks > 500).Last();
  Console.WriteLine('\nLast() :\n');  Console.WriteLine('{0,-10} {1,10} {2,5}','Name', 'ID', 'Marks');  Console.WriteLine('{0,-10} {1,10} {2,5}', last.Name, last.StudentId, last.Marks);
  Console.ReadLine(); }}
class Student{public int StudentId { get; set; }public string Name { get; set; }public int Marks { get; set; }}
```

## Output

```
All value :
Name               ID MarksSunny               4   550Ram                 5   600Krishna             6   700Anupam              7   550
First() :
Name               ID MarksSunny               4   550
Last() :
Name               ID MarksAnupam              7   550
```


---

Original Source: https://www.mindstick.com/forum/156763/how-to-find-the-first-and-last-data-of-sql-using-linq

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
