2

I have two lists

List<clsEntity> usersWithNPIRCodingPermission // It has count 159
List<clsEntity> usersWithRaiseReq // It has count 219

When doing intersect on these (there are 55 records but my below line getting empty list)

List<clsEntity> users = usersWithRaiseReq.Intersect(usersWithNPIRCodingPermission).ToList();

Why is the resulting users list empty?

Sak SK
  • 31
  • 5
  • 3
    Is that really the right code? You're talking about an intersect, but you're using the `Except` method and not `Intersect`. – Anders Abel Dec 17 '15 at 12:29
  • 6
    Does `clsEntity` implement `IEquatable`? If not, this is only going to `ReferenceEquals` for each element. You should definitely post a [mcve] so we can help. – Yuval Itzchakov Dec 17 '15 at 12:29
  • Are the additional 60 instances just repeats of other instances in the list? – David Dec 17 '15 at 12:30
  • Also Intersec and Except make use of Equalitycomparison. So if you have multiple elements that are Equal you cannot rely on Count of the list to verify your results. – CSharpie Dec 17 '15 at 12:30
  • 2
    `Intersect()` and `Except()` use `Equals()` and `GetHashCode()`, so make sure you implement those correctly, or implement an `EqualityComparer` and give that to `Intersect`/`Except` as a parameter. – Dennis_E Dec 17 '15 at 12:30
  • sorry it's not except/....I mean usersWithRaiseReq.intersect(usersWithNPIRCodingPermission).ToList(); – Sak SK Dec 17 '15 at 14:36
  • would you care to edit the question and accept the answer? – Mikey Dec 17 '15 at 14:42
  • I have edited question but I can not mark answer as I am new, have not got 15 reputations yet. – Sak SK Dec 17 '15 at 14:48

1 Answers1

4

You need to implement an equality comparer for your type if it's not a primitive e.g. int etc...

public class UsersComparer : IEqualityComparer<clsEntity>
{
    public bool Equals(clsEntity x, clsEntity y)
    {
        if (Object.ReferenceEquals(x, y)) return true;

        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        return x.ID == y.ID; // or whatever you use to determine equality
    }

    public int GetHashCode(clsEntity x)
    {
        if (Object.ReferenceEquals(x, null)) return 0;

        return x.ID.GetHashCode();
    }
}

Usage:

List<clsEntity> users = usersWithRaiseReq.Except(
      usersWithNPIRCodingPermissionn, new UsersComparer()).ToList();
DGibbs
  • 14,316
  • 7
  • 44
  • 83
  • This worked...now its getting intersect of two lists...Thank you so much for your help – Sak SK Dec 17 '15 at 14:37
  • If this answers your question consider [accepting it as the answer](https://www.google.co.uk/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjd2r71k-PJAhUKuxQKHQHqBuMQFggcMAA&url=http%3A%2F%2Fmeta.stackexchange.com%2Fquestions%2F5234%2Fhow-does-accepting-an-answer-work&usg=AFQjCNFG4vSQJ4mHJvESivMd4xw8UphR_Q&bvm=bv.110151844,d.ZWU) – DGibbs Dec 17 '15 at 14:58
  • Once I get 15 reputations I will mark it as answer as It's not allowing me to mark for now..thanks for your help again – Sak SK Dec 17 '15 at 15:19