0

我有一个包含基本代码和一系列位置的人员列表。我需要消除列表中具有相同位置的不同基码的人员,并保留具有不同位置的人员。

我尝试使用 IEqualityComparer,并在 linq 中分组,但我没有成功。你们能告诉我怎么做吗?这是我的班级结构

public class Person
{
    public string Name { get; set; }

    public List<Location> Locations { get; set; }
}
public class Location
{
    public string Name { get; set; }
    public string BaseCode { get; set; }
}

数据示例

Person 1
Name : John

Locations :
      [0]  Name : India , BaseCode : "AA12"
      [1] Name : USA ,BaseCode : "AA14"
Person 2
Name : John

Locations :
      [0]  Name : India, BaseCode : "AA13"
      [1] Name : USA ,BaseCode : "AA14"
Person 3
Name : John

Locations :
      [0]  Name : India, BaseCode : "AA16"
      [1] Name : UK , BaseCode : "AA17"

我想从我的列表中过滤掉第 2 个人并保留第 1 个人和第 3 个人。请指教

4

4 回答 4

1

免责声明:此解决方案并未专门处理BaseCode不同/相同位置的相同问题;您在要求中没有提到任何有关此的内容。


IEqualityComparer<T>路线

这里的重要部分是 和的IEqualityComparer<T>实现:PersonLocation

class Program
{
    static void Main(string[] args)
    {
        var p1 = new Person {Name ="John", BaseCode="AA12", Locations = new List<Location>
        {
            new Location { Name = "India" },
            new Location { Name = "USA" }
        }};

        var p2 = new Person {Name ="John", BaseCode="AA13", Locations = new List<Location>
        {
            new Location { Name = "India" },
            new Location { Name = "USA" }
        }};

        var p3 = new Person {Name ="John", BaseCode="AA14", Locations = new List<Location>
        {
            new Location { Name = "India" },
            new Location { Name = "UK" }
        }};

        var persons = new List<Person> { p1, p2, p3 };

        // Will not return p2.
        var distinctPersons = persons.Distinct(new PersonComparer()).ToList();

        Console.ReadLine();
    }
}

public class PersonComparer : IEqualityComparer<Person>
{
    public bool Equals(Person x, Person y)
    {
        if (x == null || y == null)
            return false;

        bool samePerson = x.Name == y.Name;

        bool sameLocations = !x.Locations
            .Except(y.Locations, new LocationComparer())
            .Any();

        return samePerson && sameLocations;
    }

    public int GetHashCode(Person obj)
    {
        return obj.Name.GetHashCode();
    }
}

public class LocationComparer : IEqualityComparer<Location>
{
    public bool Equals(Location x, Location y)
    {
        if (x == null || y == null)
            return false;

        return x.Name == y.Name;
    }

    public int GetHashCode(Location obj)
    {
        return obj.Name.GetHashCode();
    }
}

使用提供PersonComparer的 linqExcept扩展LocationComparer来生成两个位置列表之间的差异列表。

然后PersonComparer输入 linqDistinct方法。


IEquatable<T>路线

如果您需要以不同BaseCode的方式计算成为“匹配”,我认为这条路线不会奏效,因为没有给您区分价值观的机会。GetHashCode

另一种解决方案是IEquatable<T>在类本身上实现并覆盖GetHashCodeDistinct然后Except将遵循此实现:

public class Person : IEquatable<Person>
{
    public string Name { get; set; }
    public string BaseCode { get; set; }
    public List<Location> Locations { get; set; }

    public bool Equals(Person other)
    {
        if (other == null)
            return false;

        bool samePerson = Name == other.Name;

        // This is simpler because of IEquatable<Location>
        bool sameLocations = !Locations.Except(other.Locations).Any();

        return samePerson && sameLocations;
    }

    public override int GetHashCode()
    {
        return Name.GetHashCode();
    }
}

public class Location : IEquatable<Location>
{
    public string Name { get; set; }

    public bool Equals(Location other)
    {
        if (other == null)
            return false;

        return Name == other.Name;
    }

    public override int GetHashCode()
    {
        return Name.GetHashCode();
    }
}

这导致更简单的调用:

var distinctPersons = persons.Distinct().ToList();
于 2012-07-10T13:34:46.173 回答
0

我很想写如下内容。我没有检查过它的y.Locations.Equals()工作原理,但是用做同样工作的东西替换它应该很简单。

    List<Person> personList = new List<Person>();
    List<Person> deduplicatedPersonList = new List<Person>();
    personList.ForEach(x =>
    {
        Person existingPerson = personList.Find(y =>
        {
            if (y.Locations.Equals(x.Locations))
                return false;
            return true;
        });
        if (existingPerson == null)
            deduplicatedPersonList.Add(x);
    });
于 2012-07-10T13:36:58.727 回答
0

您可以使用 IEquatable 接口,并像这样覆盖 Equal 和 GetHashCode 方法:

在问题更改后编辑:

public class Location : IEquatable<Location>
{    
       public string Name { get; set; }     
       public string BaseCode { get; set; 

        public bool Equals(Location other)
        {
            if (Object.ReferenceEquals(other, null)) return false;

            if (Object.ReferenceEquals(this, other)) return true;
            return BaseCode.Equals(other.BaseCode);
        }

        public override int GetHashCode()
        {
            return BaseCode.GetHashCode();
        }


} 

因此,现在您可以在人员列表上使用 Distinct,它只会返回不同的名称和 BaseCode。

 var distinctListPerson = PersonList.Distinct().ToList();

您可以从MSDN阅读有关此接口的信息

于 2012-07-10T13:28:52.387 回答
0

亚当的解决方案是更“正确”的处理方式。但是如果你想用 LINQ 来做,那么像这样的东西也应该做(注意,代码需要对位置进行排序并将字符串作为标识符):

persons
    .GroupBy(x => x.Name)
    .SelectMany(x => x)
        .GroupBy(y => string.Concat(y.Locations.Select(z => z.Name)))
    .SelectMany(x => x
        .GroupBy(y => string.Concat(y.Locations.Select(z => z.BaseCode)))
    .Select(x => x.First());
于 2012-07-10T13:34:45.390 回答