Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C# Data Types
  • C# Decision Making
  • C# Methods
  • C# Delegates
  • C# Constructors
  • C# Arrays
  • C# ArrayList
  • C# String
  • C# Tuple
  • C# Indexers
  • C# Interface
  • C# Multithreading
  • C# Exception
Open In App
Next Article:
LINQ | Element Operator | LastOrDefault
Next article icon

LINQ | Element Operator | First

Last Updated : 24 May, 2019
Comments
Improve
Suggest changes
Like Article
Like
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

First Operator

The First operator is used to return the first element of the given collection or sequence. Or it can also return the first element according to the given condition. This method can be overloaded in two different ways:
  • First<TSource>(IEnumerable<TSource>): This method returns the first element of the given sequence or collection without any condition.
  • First<TSource>(IEnumerable<TSource>, Func<TSource, Boolean>): This method returns the first element which specifies the given condition.
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.
  • If the given collection or sequence does not contain any element in it, then this method will throw an InvalidOperationException.
  • It will throw an error if the given collection or sequence does not contain any element that satisfies the given condition or it contains null.
Example 1: CSharp
// C# program to illustrate the  // use of First operator using System; using System.Linq;  class GFG {      // Main Method     static public void Main()     {          // Data source         int[] sequence1 = {112, 44, 55,                        66, 77, 777, 56};          // Get the element which specifies          // the given condition         // Using First function         var result1 = sequence1.First(seq => seq > 77);         Console.WriteLine(result1);          // If you try to run the commented part then         // this part will give you InvalidOperationException         // because the given sequence does not contain any element         /*            int[] sequence2 = {};            var result2 = sequence2.First();            Console.WriteLine(result2);         */     } } 
Output:
  112  
Example 2: CSharp
// C# program to find the name  // of the first 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 = "Anju", emp_gender = "Female",                                   emp_hire_date = "17/6/2015", emp_salary = 50000},         };          // Query to find the name         // the first employee Using         // First method         var res = emp.Select(e => e.emp_name).First();         Console.WriteLine("Employee Name: {0}", res);     } } 
Output:
  Employee Name: Anjita  

Next Article
LINQ | Element Operator | LastOrDefault
author
ankita_saini
Improve
Article Tags :
  • C#
  • CSharp LINQ

Similar Reads

  • LINQ | Element Operator | Last
    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 princip
    3 min read
  • LINQ | Element Operator | FirstOrDefault
    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 princip
    4 min read
  • LINQ | Element Operator | ElementAt
    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 princip
    3 min read
  • LINQ | Element Operator | LastOrDefault
    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 princip
    3 min read
  • LINQ | Element Operator | Single
    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 princip
    3 min read
  • LINQ | Element Operator | ElementAtOrDefault
    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 princip
    3 min read
  • LINQ | Set Operator | Intersect
    In LINQ, Set operators are those operators in query expression which return a result set which is based on the existence or non-existence of the equivalent elements within the same or different collections or sequences or sets. The standard query operator contains the following set operators: Union
    3 min read
  • LINQ | Generation Operator | DefaultIfEmpty
    The generation operators are used for creating a new sequence of values. The Standard Query Operator supports 4 different types of generation operators: DefaultIfEmpty Empty Range Repeat DefaultIfEmpty Operator The DefaultIfEmpty operator is used to replace an empty collection or sequence with a def
    3 min read
  • LINQ | Set Operator | Except
    In LINQ, Set operators are those operators in query expression which return a result set based on the existence or non-existence of the equivalent elements within the same or different collections or sequences or sets. The standard query operator contains the following set operators: Union Intersect
    3 min read
  • LINQ | Element Operator | SingleOrDefault
    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 princip
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences