How to compare two lists by property?

Asked

Viewed 128 times

3

I have two student lists. I would like to compare them by the attribute Name, and return only the unique values of the student. In the example below, return only Aluno01.Name = "5555".

        List<Aluno1> aluno01 = new List<Aluno1>();
        aluno01.Add(new Aluno1 { Id = 1, Nome = "1111" });
        aluno01.Add(new Aluno1 { Id = 2, Nome = "2222" });
        aluno01.Add(new Aluno1 { Id = 2, Nome = "5555" });

        List<Aluno2> aluno02 = new List<Aluno2>();
        aluno02.Add(new Aluno2 { Id = 1, Nome = "1111" });
        aluno02.Add(new Aluno2 { Id = 2, Nome = "2222" });
        aluno02.Add(new Aluno2 { Id = 2, Nome = "3333" });
        aluno02.Add(new Aluno2 { Id = 2, Nome = "4444" });

Grateful,

1 answer

4


You can use using System.Linq;

var intersect = aluno01.Where(a => !aluno02.Select(b => b.Nome).Contains(a.Nome));

See working in dotnetfiddle

  • Perfect, that was it!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.