About
Index is one of feature introduced in .NET LINQ. By using this method, we can easily extract implicit index of IEnumerable.
The other .NET 9 LINQ features are as follows.
The Index method returns index and index of an item in following format.
Format: IEnumerable<(int Index, TSource Item)>
Scenario
Lets consider to display employee name in list and their respective order.
Employee class
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
}
Setup Employee List with Sample Data
var employees = new List<Employee> {
new Employee(){ Id = 11, Name = "Ram" },
new Employee(){ Id = 12, Name = "Bheem" },
new Employee(){Id = 13, Name = "Lakshman"},
new Employee(){Id = 14, Name = "Hanu"},
new Employee(){Id = 15, Name = "Dev"},
new Employee(){Id = 16, Name = "Nandan"},
new Employee(){Id = 17, Name = "Krish"},
new Employee(){Id = 18, Name = "Hash"},
};
foreach ((int index, Employee emp) in employees.Index())
{
Console.WriteLine($"Index: {index}, Employee Name: {emp.Name}");
}
Screenshots
The following code and output screenshots gives easy understanding and for your implementation.
No comments:
Post a Comment