linq object list contains
using System.Linq;
bool exists = list.Any(x => x.surname == "Smith");
linq object list contains
using System.Linq;
bool exists = list.Any(x => x.surname == "Smith");
c# does value exist in list
public int GetItemFromList() {
List<Item> list = new List<Item>(
new Item(1),
new Item(2),
new Item(3)
);
Item testItem = new Item(1);
// Inside FindIndex() you can specify a lambda expression where you
// query if an item exists like a boolean test.
int index = list.FindIndex(item => testItem.Id == item.Id);
// in this case out testItem.Id (1) is equal to an item in the list
if (index > -1)
{
// We get here with the index 0!
return index;
}
}
public class Item
{
public int Id { get; set; }
public Item() { }
public Item(int id)
{
Id = id;
}
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us