Summary: in this tutorial, you’ll learn how to use the LINQ Single()
method to return a single element of a sequence that satisfies a condition.
Introduction to the LINQ Single() method
The LINQ Single()
method returns one and only one element of a sequence that satisfies a specified condition.
Here’s the syntax of the Single()
method:
public static TSource Single<TSource> (
IEnumerable<TSource> source,
Func<TSource,bool> predicate
);
Code language: C# (cs)
In this syntax:
TSource
is the type of elements of thesource
sequence.source
is anIEnumerable<T>
that contains elements.predicate
is a function that tests an element for a condition.
The Single()
method returns a single element with the type TSource
of the source
sequence.
If two or more such elements or no element exists, the Single()
method throws an exception. If either source
or predicate
is null
, the Single()
method throws an ArgumentNullException
.
The Single()
method has an overload that doesn’t have any argument:
public static TSource Single<TSource> (
IEnumerable<TSource> source
);
Code language: C# (cs)
In this case, the Single()
method returns a single element of the source
sequence. If the source
sequence has more than one element, the Single()
method will throw an InvalidOperationException
.
LINQ Single() example
The following program demonstrates how to use the Single()
method to select a single employee from an employee list:
using static System.Console;
class Employee
{
public int? Id { get; set; }
public string? Name { get; set; }
public int Salary { get; set; }
}
class Program
{
public static void Main()
{
var employees = new List<Employee>()
{
new Employee { Id = 1, Name = "John", Salary = 50000 },
new Employee { Id = 2, Name = "Jane", Salary = 60000 },
new Employee { Id = 3, Name = "Bob", Salary = 45000 },
new Employee { Id = 4, Name = "Sara", Salary = 55000 },
new Employee { Id = 5, Name = "Tom", Salary = 65000 }
};
var employee = employees.Single(e => e.Id == 1);
WriteLine($"{employee.Id} - {employee.Name}");
}
}
Code language: C# (cs)
Output:
1 - John
Code language: C# (cs)
How it works.
First, define the Employee
class that has three properties Id
, Name
, and Salary
:
class Employee
{
public int? Id { get; set; }
public string? Name { get; set; }
public int Salary { get; set; }
}
Code language: C# (cs)
Second, create a new list of employees:
var employees = new List<Employee>()
{
new Employee { Id = 1, Name = "John", Salary = 50000 },
new Employee { Id = 2, Name = "Jane", Salary = 60000 },
new Employee { Id = 3, Name = "Bob", Salary = 45000 },
new Employee { Id = 4, Name = "Sara", Salary = 55000 },
new Employee { Id = 5, Name = "Tom", Salary = 65000 }
};
Code language: C# (cs)
Third, select a single employee with id 1 by using the Single()
method. The Single()
method returns a single Employee
object:
var employee = employees.Single(e => e.Id == 1);
Code language: C# (cs)
Finally, write the Employee
object to the console:
WriteLine($"{employee.Id} - {employee.Name}");
Code language: C# (cs)
SingleOrDefault method
If you want to return a default value if no element in a sequence satisfies a condition, you can use the SingleOrDefault()
method:
public static TSource? SingleOrDefault<TSource> (
this IEnumerable<TSource> source,
Func<TSource,bool> predicate,
TSource defaultValue
);
Code language: C# (cs)
For example:
using Models;
using static System.Console;
class Employee
{
public int? Id { get; set; }
public string? Name { get; set; }
public int Salary { get; set; }
}
class Program
{
public static void Main()
{
var employees = new List<Employee>()
{
new Employee { Id = 1, Name = "John", Salary = 50000 },
new Employee { Id = 2, Name = "Jane", Salary = 60000 },
new Employee { Id = 3, Name = "Bob", Salary = 45000 },
new Employee { Id = 4, Name = "Sara", Salary = 55000 },
new Employee { Id = 5, Name = "Tom", Salary = 65000 }
};
var employee = employees.SingleOrDefault(e => e.Id == 10, null);
if (employee != null)
{
WriteLine($"{employee.Id} - {employee.Name}");
}
else
{
WriteLine("The element was not found.");
}
}
}
Code language: C# (cs)
Output:
The element was not found.
Code language: C# (cs)
In this example, the employees
list has no Employee
object with id 10. Therefore, the SingleOrDefault()
method returns null
as the default value.
Summary
- Use LINQ
Single()
method to return a single element of a sequence that satisfies a condition.