📅  最后修改于: 2022-03-11 14:48:50.678000             🧑  作者: Mango
// Lets say you have an object with tha name of 'Role' nad it has parameters
// 'Name' & 'Id'
List roles = new List();
roles.Add(new Role() { Name = "Admin", Id = 1 });
roles.Add(new Role() { Name = "User", Id = 2 });
// Using a lambda expression we can find the index of a role
// with a specific text:
int roleIndex = roles.FindIndex(
// Note that you can also use the index if youd like
role => role.Name == "Admin"
);
// This can also be done using a variable from outside
// the lambda expression:
string name = "Admin";
int roleIndex = roles.FindIndex(
// Note that you can also use the index if youd like
role => role.Name == "name
);
if (roleIndex > -1)
{
Console.log($"Role {roles[roleIndex].Name} +
was found at index {roleIndex}");
}