Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C# Linq find unique item

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could use the linq operators Where and Count like

var uniqueItems = list.Where(x => list.Count(item => item.name == x.name) == 1);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading