Open In App

LINQ | Element Operator | Single

Improve
Improve
Like Article
Like
Save
Share
Report

The element operators are used to return a single, or a specific element from the sequence or collection. For example, in a school when we ask, who is the principal? Then there will be only one person that will be the principal of the school. So the number of students is a collection and the principal is the only result that comes from the collection.

The LINQ Standard Query Operator supports 8 types of element operators:

  1. ElementAt
  2. ElementAtOrDefault
  3. First
  4. FirstOrDefault
  5. Last
  6. LastOrDefault
  7. Single
  8. SingleOrDefault

Single Operator

The single operator is used to return the single element of the collection or sequence. Or it returns the single element which specifies the given condition. This method can be overloaded in two different ways:

  • Single <TSource>(IEnumerable <TSource>, Func<TSource, Boolean>): This method returns the only element of the collection or sequence which specifies the given condition and will throw an exception if more than one element exists that specifies the given condition.
  • Single<TSource>(IEnumerable<TSource>): This method returns the only element of a the given sequence or collection, and throws an exception if there is not exactly one element in the sequence or collection.

Important Points:

  • It does not support query syntax in C# and VB.Net languages.
  • It support method syntax in both C# and VB.Net languages.
  • It present in both the Queryable and Enumerable class.
  • It throw an InvalidOperationException if the sequence does not contain the element which specifies the given condition, or it contains more than one element that specifies the given condition, or the sequence is empty.

Example 1:




// C# program to illustrate the
// use of Single operator
using System;
using System.Linq;
using System.Collections.Generic;
  
class GFG {
  
    static public void Main()
    {
  
        // Data source
        int[] sequence1 = {112, 44, 55, 66, 77, 777, 56};
        int[] sequence2 = {2};
  
        // Get the element which specifies the given 
        // condition Using Single(Condition) function
        var result1 = sequence1.Single(seq => seq == 112);
          
        Console.WriteLine("Element: {0}", result1);
  
        // Get the only element of the 
        // sequence Using Single() function
        var result2 = sequence2.Single();
          
        Console.WriteLine("Element: {0}", result2);
  
        // This commented part gives you exception
        // because the given sequence does not 
        // contain elements
          
        /*
            int[] sequence3 = {};
            var result3 = sequence3.Single();
            Console.WriteLine("Element: {0}", result3);
        */
    }
}


Output:

Element: 112
Element: 2

Example 2:




// C# program to find the 
// name of the employee
using System;
using System.Linq;
using System.Collections.Generic;
  
// Employee details
public class Employee {
  
    public int emp_id
    {
        get;
        set;
    }
  
    public string emp_name
    {
        get;
        set;
    }
  
    public string emp_gender
    {
        get;
        set;
    }
  
    public string emp_hire_date
    {
        get;
        set;
    }
  
    public int emp_salary
    {
        get;
        set;
    }
}
  
class GFG {
  
    // Main method
    static public void Main()
    {
        List<Employee> emp = new List<Employee>() {
              
            new Employee() {emp_id = 209, emp_name = "Anjita", emp_gender = "Female",
                                    emp_hire_date = "12/3/2017", emp_salary = 20000},
                          
            new Employee() {emp_id = 210, emp_name = "Soniya", emp_gender = "Female",
                                    emp_hire_date = "22/4/2018", emp_salary = 30000},
  
            new Employee() {emp_id = 211, emp_name = "Rohit", emp_gender = "Male",
                                  emp_hire_date = "3/5/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 212, emp_name = "Supriya", emp_gender = "Female",
                                      emp_hire_date = "4/8/2017", emp_salary = 40000},
  
            new Employee() {emp_id = 213, emp_name = "Anil", emp_gender = "Male",
                                emp_hire_date = "12/1/2016", emp_salary = 40000},
  
            new Employee() {emp_id = 214, emp_name = "Anupriya", emp_gender = "Female",
                                         emp_hire_date = "17/6/2015", emp_salary = 50000},
        };
  
        // Query to find the name the 
        // employee Using Single method
        var res = emp.Single(e => e.emp_name == "Anjita");
          
        string val = res.emp_name;
          
        Console.WriteLine("Employee name: {0}", val);
    }
}


Output:

Employee name: Anjita


Last Updated : 24 May, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads