C# Using Enumerable Range and Except with custom class to determine missing sequence number
Posted
by Jon
on Stack Overflow
See other posts from Stack Overflow
or by Jon
Published on 2010-03-19T09:02:28Z
Indexed on
2010/03/19
9:11 UTC
Read the original article
Hit count: 354
I have a List<MyClass>
The class is like this:
private class MyClass
{
public string Name{ get; set; }
public int SequenceNumber { get; set; }
}
I want to work out what Sequence numbers might be missing. I can see how to do this here however because this is a class I am unsure what to do?
I think I can handle the except method ok with my own IComparer but the Range method I can't figure out because it only excepts int so this doesn't compile:
Enumerable.Range(0, 1000000).Except(chqList, MyEqualityComparer<MyClass>);
Here is the IComparer:
public class MyEqualityComparer<T> : IEqualityComparer<T> where T : MyClass
{
#region IEqualityComparer<T> Members
public bool Equals(T x, T y)
{
return (x == null && y == null) || (x != null && y != null && x.SequenceNumber.Equals(y.SequenceNumber));
}
/// </exception>
public int GetHashCode(T obj)
{
if (obj == null)
{
throw new ArgumentNullException("obj");
}
return obj.GetHashCode();
}
#endregion
}
© Stack Overflow or respective owner