Search code examples
c#linq

LINQ Except is not retrieving the duplicates


I have two list in c#, and I want to retrieve all elements on listaA that are not present in listaB, so I decided to use Except method like the code bellow:

List<string> listaA = new List<string>() { "a", "b", "b", "c", "d", "e", "e" };
List<string> listaB = new List<string>() { "e" };

var inter = listaA.Intersect(listaB);
var excep = listaA.Except(listaB).Count();

except output -> 4

But my expected output should be 5 since we have two "b", so if in the ListaA we exclude all the elements that are present in listaA, why the output is 4 instead of 5, and how can I put it working as I expect?


Solution

  • You can do Count of the output:

    var newcount = listaA.Count(x =>!listaB.Contains(x));