Your current way can only get different data that exists in List1 but not in List2.
To get all their different data, you also need to reverse except
and merge them by Union
.
Here is my class Product:
public class Product
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
}
Here is the code:
List<Product> List1 = new List<Product>()
{
new Product(){ Id =1, Title="aaa", Description="cccccccc" },
new Product(){ Id =2,Title="bbb", Description="cccccccc" },
new Product(){ Id =3, Title="ccc", Description="cccccccc" },
};
List<Product> List2 = new List<Product>()
{
new Product(){ Id =1, Title="aaa" , Description ="csdfsdf"},
new Product(){ Id =4, Title="bbb" , Description ="cccccccccc"},
};
var newList= (List1.Select(x => new { x.Id, x.Title }).Except(List2.Select(x => new { x.Id, x.Title })).
Union(List2.Select(x => new { x.Id, x.Title }).Except(List1.Select(x => new { x.Id, x.Title })))).ToList();
Here is the test process:
