2

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
        }

1 Answer 1

5

You could always project the class to the sequence number, as that's what you're interested in from the class, which will give you an IEnumerable<int> that can be directly excepted from the range, e.g.

Enumerable.Range(0, 1000000).Except(chqList.Select(c => c.SequenceNumber));

This will return the missing sequence numbers. Note that this assumes chqList is a list of MyClass objects.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! I'll fully understand LINQ one day!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.