質問

How do I remove a duplicate listing?

My class :

public class test
{
    public string UserName { get; set; }
    public List<int> Scores { get; set; }
}

Sample data

var tests = new List<test> {
                new test
                {
                    Scores = new List<int> { 1, 2, 3 },
                    UserName = "user1"
                },
                new test
                {
                    Scores = new List<int> { 1, 5, 3 },
                    UserName = "user2"
                },
                new test
                {
                    Scores = new List<int> { 1, 2, 3 },
                    UserName = "user3"
                }
            };

I need to remove duplicates based on the score.

For example, the above list is repeated scores of the two users.I need to remove one of the duplicated users. No matter what the user is removed.Only one is removed. I have no idea to do it.

Scenario : user1 and user3 is Repeated.

user1 or user3 Should be removed.One of the two should to stay

役に立ちましたか?

解決

Following query groups tests by first test instance, which have same scores. Then you just select group keys:

from t in tests
group t by tests.First(x => Enumerable.SequenceEqual(x.Scores, t.Scores)) into g
select g.Key;

Returns user1 and user2. Same with lambda syntax:

tests.GroupBy(t => tests.First(x => Enumerable.SequenceEqual(x.Scores,t.Scores)))
     .Select(g => g.Key);

If you want last user to stay, then change First to Last, but First is more efficient here, because it stops enumeration earlier.

他のヒント

You could use Linq function Distinct() (tests.Distinct(...)) and provide a IEqualityComparer instance that checks if your scores are identical

There are 2 ways you can do this:

  1. Make a for loop and remove double entries:

    var namesFound = new List<string>();
    for (int i = 0; i < tests.Count; i++)
    {
       if (!namesFound.Contains(tests[i].UserName))
       {
           namesFound.Add(tests[i].UserName);
       }
       else
       {
           tests.Remove(tests[i]);
           i--;
       }
    }
    
  2. Implement IEqualityComparer on your class test. Check this link: http://msdn.microsoft.com/en-us/library/ms132151(v=vs.110).aspx
    After that check for equality by using tests[0] == tests[1] and remove the item when they are equal

Provide a custom IEqualityComparer<test> to Distinct method and get the job done.

internal class TestComparer : IEqualityComparer<test>
{
    public bool Equals(test x, test y)
    {
        if (x.Scores.Count != y.Scores.Count)
            return false;
        return new HashSet<int>(x.Scores).SetEquals(y.Scores);
    }
    public int GetHashCode(test obj)
    {
        return obj.Scores.Aggregate((seed,x)=> seed | x);
    }
}

var filteredList = tests.Distinct(new TestComparer()).ToList();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top