I have the classes:
class SomeClass
{
public string Name{get;set;}
public int SomeInt{get;set;}
}
class SomeComparison: IEqualityComparer<SomeClass>
{
public bool Equals(SomeClass s, SomeClass d)
{
return s.Name == d.Name;
}
public int GetHashCode(SomeClass a)
{
return (a.Name.GetHashCode() * 251);
}
}
I also have two large List<SomeClass>
called list1
and list2
before I used to have:
var q = (from a in list1
from b in list2
where a.Name != b.Name
select a).ToList();
and that took about 1 minute to execute. Now I have:
var q = list1.Except(list2,new SomeComparison()).ToList();
and that takes less than 1 second!
I will like to understand what does the Except method do. Does the method creates a hash table of each list and then perform the same comparison? If I will be performing a lot of this comparisons should I create a Hashtable instead?
EDIT
Now instead of having lists I have two HashSet<SomeClass>
called hashSet1
and hashSet2
when I do:
var q = (from a in hashSet1
form b in hashSet2
where a.Name != b.Name
select a).ToList();
that still takes a long time... What am I doing wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…