---
title: "What is the difference between the first and single functions in LINQ?"  
description: "What is the difference between the first and single functions in LINQ?"  
author: "Ethan Karla"  
published: 2021-09-24  
updated: 2021-09-24  
canonical: https://www.mindstick.com/forum/156764/what-is-the-difference-between-the-first-and-single-functions-in-linq  
category: "linq"  
tags: ["c#", "ado.net", "linq"]  
reading_time: 2 minutes  

---

# What is the difference between the first and single functions in LINQ?

What is the [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) between the first and single [functions](https://www.mindstick.com/forum/160140/explain-the-role-of-functions-as-a-service-faas-in-serverless-computing) in [LINQ](https://www.mindstick.com/articles/12007/language-integrated-query-linq-queries)?

## Replies

### Reply by Ravi Vishwakarma

## First and Single operator difference

## First

1. The collection has no element then throw NoElementsException.
2. The first function returns the first row from or top one row from the collection.
3. The first method takes a condition to fetch elements from the collection

## Single

1. The collection has no element then throw NoElementsException and the element has more than one then return MoreThanOneElementException.
2. The single function has checked the collection that element is single then return row.
3. The single method takes a condition but this condition allows one row, no more than one row.

## Example

```
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 }
  } ;
  Student first = students.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 first = students.Where( stu => stu.Marks > 500).Single();
  // this will throw an InvalidOperationException exception because more than 1 element in students.
  Student last = students.Single( stu => stu.StudentId == 2);
  Console.WriteLine('\nSingle() :\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

```
First() :
Name               ID MarksAshu                1   500
Single() :
Name               ID MarksShyam               2   300
```


---

Original Source: https://www.mindstick.com/forum/156764/what-is-the-difference-between-the-first-and-single-functions-in-linq

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
