C# Collections

RameshLathu
RameshLathu used Ask the Experts™
on
Hi Experts,

I have a collection (Collection<PolicyTransaction> LobjThreadPolicyTransactions).

This collection is having a string variable called "PolicyNumber"

In thins list of collection(LobjThreadPolicyTransactions), sometimes, I will be getting same PolicyNumber more than once (Same policy number may have more than one transaction).

I want to check which policy number is repeating and want to keep those records in a separate list.

Basically, I want to get the second repeated item from the collection in VS 2005, in C#.

How to do this? Please help.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Are you permitted to use LINQ?

Author

Commented:
No
ǩa̹̼͍̓̂ͪͤͭ̓u͈̳̟͕̬ͩ͂̌͌̾̀ͪf̭̤͉̅̋͛͂̓͛̈m̩̘̱̃e͙̳͊̑̂ͦ̌ͯ̚d͋̋ͧ̑ͯ͛̉Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Sorry, I missed that you said this was for VS 2005. I guess that would make sense then  = )

See if this gives you the result you seek:

List<PolicyTransaction> repeats = new List<PolicyTransaction>();
List<PolicyTransaction> nonrepeats = new List<PolicyTransaction>();
PolicyNumberEqualityComparer comparer = new PolicyNumberEqualityComparer();

foreach (PolicyTransaction tran in LobjThreadPolicyTransactio)
{
    if (repeats.Contains(tran, comparer))
    {
        repeats.Add(tran);

        continue;
    }

    bool repeated = false;

    for (int i = nonrepeats.Count - 1; i >= 0; i--)
    {
        if (comparer.Equals(nonrepeats[i], tran))
        {
            repeats.Add(tran);
            repeats.Add(nonrepeats[i]);
            nonrepeats.RemoveAt(i);
            repeated = true;

            break;
        }
    }

    if (!repeated)
    {
        nonrepeats.Add(tran);
    }
}

Open in new window


With the PolicyNumberEqualityComparer defined as:

class PolicyNumberEqualityComparer : IEqualityComparer<PolicyTransaction>
{
    public bool Equals(PolicyTransaction x, PolicyTransaction y)
    {
        return string.Equals(x.PolicyNumber, y.PolicyNumber);
    }

    public int GetHashCode(PolicyTransaction obj)
    {
        return obj.GetHashCode();
    }
}

Open in new window


Make sure you "using" the System.Collections.Generic namespace.
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

Author

Commented:
Thank you, I will check it out and let you know.

Author

Commented:
Hi,

I tried to add the code, but the "Contains" is not accepting two parameters. (if (repeats.Contains(tran, comparer))

Author

Commented:
In 2010, contains will accept two parameter, one is the object and second paramter as comparer. But, I am not getting that option in 2005.
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
So it does. You can add a method to mimic the behavior:

bool TheListContains(IEnumerable<PolicyTransaction> theList, PolicyTransaction transaction, IEqualityComparer<PolicyTransaction> comparer)
{
    foreach (PolicyTransaction tran in theList)
    {
        if (comparer.Equals(tran, transaction))
        {
            return true;
        }
    }

    return false;
}

Open in new window


Then change the calling code to:

...

if (TheListContains(repeats, tran, comparer))

...

Open in new window

Author

Commented:
Thankyou. Its working fine

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial