In LINQ (Language Integrated Query), element operators are used to work with individual elements of a sequence. Here’s a rundown of some common element operators. There are following element operators.
It returns the first element of a sequence that satisfies a specified condition or the first element if no condition is specified. it returns error.
using System;
using System.Collections.Generic;
using System.Linq;
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
class Program
{
static void Main()
{
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "Rohatash", IsActive = true },
new Employee { Id = 2, Name = "Mohit", IsActive = false },
new Employee { Id = 3, Name = "Saurav", IsActive = true },
new Employee { Id = 4, Name = "Jeet", IsActive = true }
};
var firstEmployee = employees.First();
Console.WriteLine(firstEmployee.Name); // Output: Rohatash
var firstEmployeecon = employees.First(e => !e.IsActive);
Console.WriteLine(firstEmployeecon.Name); // Output: Mohit
}
}
Example
It returns the first element of a sequence that satisfies a specified condition, or a default value if no such element is found.
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
class Program
{
static void Main()
{
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "Rohatash", IsActive = true },
new Employee { Id = 2, Name = "Mohit", IsActive = false },
new Employee { Id = 3, Name = "Saurav", IsActive = true },
new Employee { Id = 4, Name = "Jeet", IsActive = true }
};
var firstActiveEmployee = employees.FirstOrDefault(e => e.IsActive);
Console.WriteLine(firstActiveEmployee.Name); // Output: Rohatash
var noActiveEmployee = employees.FirstOrDefault(e => !e.IsActive);
Console.WriteLine(noActiveEmployee?.Name); // Output: Mohit
}
}
It returns the last element of a sequence that satisfies a specified condition or the last element if no condition is specified.
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
class Program
{
static void Main()
{
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "Rohatash", IsActive = true },
new Employee { Id = 2, Name = "Mohit", IsActive = false },
new Employee { Id = 3, Name = "Saurav", IsActive = true },
new Employee { Id = 4, Name = "Jeet", IsActive = true }
};
var lastEmployee = employees.Last();
Console.WriteLine(lastEmployee.Name); // Jeet
}
}
It returns the last element of a sequence that satisfies a specified condition, or a default value if no such element is found.
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
class Program
{
static void Main()
{
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "Rohatash", IsActive = true },
new Employee { Id = 2, Name = "Mohit", IsActive = false },
new Employee { Id = 3, Name = "Saurav", IsActive = true },
new Employee { Id = 4, Name = "Jeet", IsActive = true }
};
var lastActiveEmployee = employees.LastOrDefault(e => e.IsActive);
Console.WriteLine(lastActiveEmployee.Name); // Output: Jeet
}
}
It returns the only element of a sequence that satisfies a specified condition, or throws an exception if more than one element is found.
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
class Program
{
static void Main()
{
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "Rohatash", IsActive = true },
new Employee { Id = 2, Name = "Mohit", IsActive = false },
new Employee { Id = 3, Name = "Saurav", IsActive = true },
new Employee { Id = 4, Name = "Jeet", IsActive = true }
};
var singleEmployee = employees.Single(e => e.Id == 3);
Console.WriteLine(singleEmployee.Name); // Output: Saurav
// This will throw an exception because no employee has Id 5
// var nonExistentEmployee = employees.Single(e => e.Id == 5);
}
}
It returns the only element of a sequence that satisfies a specified condition, or a default value if no such element is found.
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
class Program
{
static void Main()
{
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "Rohatash", IsActive = true },
new Employee { Id = 2, Name = "Mohit", IsActive = false },
new Employee { Id = 3, Name = "Saurav", IsActive = true },
new Employee { Id = 4, Name = "Jeet", IsActive = true }
};
var singleOrDefaultEmployee = employees.SingleOrDefault(e => e.Id == 3);
Console.WriteLine(singleOrDefaultEmployee.Name); // Output: Saurav
var nonExistentEmployee = employees.SingleOrDefault(e => e.Id == 5);
Console.WriteLine(nonExistentEmployee?.Name); // Output: (No output, as the value is null)
}
}
It returns the element at a specified index in a sequence.
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
class Program
{
static void Main()
{
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "Rohatash", IsActive = true },
new Employee { Id = 2, Name = "Mohit", IsActive = false },
new Employee { Id = 3, Name = "Saurav", IsActive = true },
new Employee { Id = 4, Name = "Jeet", IsActive = true }
};
var employeeAtIndex = employees.ElementAt(2);
Console.WriteLine(employeeAtIndex.Name); // Output: Saurav
var employeeAtIndex1 = employees.ElementAt(10);
Console.WriteLine(employeeAtIndex.Name); // Output: Error
}
}
It returns the element at a specified index in a sequence, or a default value if the index is out of range. returns null if the index is out of range.
Example
using System;
using System.Collections.Generic;
using System.Linq;
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsActive { get; set; }
}
class Program
{
static void Main()
{
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "Rohatash", IsActive = true },
new Employee { Id = 2, Name = "Mohit", IsActive = false },
new Employee { Id = 3, Name = "Saurav", IsActive = true },
new Employee { Id = 4, Name = "Jeet", IsActive = true }
};
var employeeAtIndexOrDefault = employees.ElementAtOrDefault(2);
Console.WriteLine(employeeAtIndexOrDefault.Name); // Output: Saurav
var outOfRangeEmployee = employees.ElementAtOrDefault(10);
Console.WriteLine(outOfRangeEmployee?.Name); // Output: (No output, as the value is null)
}
}