📜  LINQ |元素运算符| ElementAt

📅  最后修改于: 2021-05-29 21:09:23             🧑  作者: Mango

元素运算符用于从序列或集合中返回单个或特定元素。例如,当我们问学校时,校长是谁?这样一来,只有一人成为学校的校长。因此,学生人数是一个集合,而校长是唯一来自集合的结果。

LINQ标准查询运算符支持8种类型的元素运算符:

  1. ElementAt
  2. ElementAtOrDefault
  3. 第一的
  4. 第一或默认
  5. 最后的
  6. LastOrDefault
  7. 单身的
  8. 单一或默认

ElementAt运算符

ElementAt运算符用于从给定的集合或序列中的特定索引返回元素。这里指定的索引是从零开始的索引。假设一个数组包含3个元素,即1、2、3,并且我们要在索引1处打印值,所以我们使用ElementAt方法,因为它将返回在索引1(即2)处存在的元素。

重要事项:

  • 它不支持C#和VB.Net语言的查询语法。
  • 它支持C#和VB.Net语言的方法语法。
  • 它同时存在于Queryable和Enumerable类中。
  • 如果给定索引超出范围,则此方法将抛出ArgumentOutOfRangeException

范例1:

// C# program to illustrate the
// use of ElementAt operator
using System;
using System.Linq;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Data source
        string[] sequence = {"Dog", "Cat", "Cow",
                               "Goat", "Parrot"};
  
        // Display the sequences
        Console.WriteLine("Sequence is: ");
  
        foreach(var s in sequence)
        {
            Console.WriteLine(s);
        }
  
        // Get element at index 3
        // Using ElementAt function
        var result = sequence.ElementAt(3);
  
        Console.WriteLine("Element is: {0}", result);
    }
}
输出:
Sequence is: 
Dog
Cat
Cow
Goat
Parrot
Element is: Goat

范例2:

// C# program to find the 
// ID 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 emp = new List() {
              
            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 ID of 
        // the employee at index 1
        // Using ElementAt method
        var res = emp.Select(e => e.emp_id).ElementAt(1);
          
        Console.WriteLine("Employee ID: {0}", res);
    }
}
输出:
Employee ID: 210