I’m looking for a way to find items in a list that are unique:
List<Item> list = new List<Item>();
list.Add(new Item() { id = 1, name = "test" });
list.Add(new Item() { id = 2, name = "test" });
list.Add(new Item() { id = 3, name = "test1"});
list.Add(new Item() { id = 4, name = "test2"});
list.Add(new Item() { id = 5, name = "test2"});
var uniqueItems = ?
uniqueItems should contain { id = 3, name = "test1"} because test1 is unique
Whats the best way to implement that?
>Solution :
You could use the linq operators Where and Count like
var uniqueItems = list.Where(x => list.Count(item => item.name == x.name) == 1);