根据所选列从列表中删除重复的行?

时间:2017-11-08 05:42:30

标签: c# linq lambda

我有一个类列表,这个类中有两列。现在我想使用特定列从该类中删除重复的行。就像从第一列中删除重复一样,仅从发送列中删除或从两者中删除。所以我使用以下代码。有没有最好的方法来完成这个过程,因为将来我将在这个类中有20-25列,那时我必须在这个函数中添加20-25 if语句?

public List<ContactTemp> RemoveDupliacacse(List<ContactTemp> ContactTempList, List<string> objcolumn)
{
    List<ContactTemp> ContactTempListRemobdup = new List<ContactTemp>();
    if (objcolumn.Contains("CITY"))
    {
        ContactTempListRemobdup = ContactTempList.GroupBy(s => s.City).Select(group => group.First()).ToList();
    }
    if (objcolumn.Contains("STATE"))
    {
        ContactTempListRemobdup = ContactTempList.GroupBy(s => s.State).Select(group => group.First()).ToList();
    }
    return ContactTempListRemobdup;
}

3 个答案:

答案 0 :(得分:0)

这样的事情怎么样?

public static List<ContactTemp> RemoveDupliacacse(
    List<ContactTemp> ContactTempList, 
    IEnumerable<Func<ContactTemp, object>> columnSelectors)
{
    IEnumerable<ContactTemp> ContactTempListRemobdup = ContactTempList;

    foreach(var selector in columnSelectors)
    {
        ContactTempListRemobdup = ContactTempListRemobdup
            .GroupBy(s => selector(s))
            .Select(group => group.First());
    }

    return ContactTempListRemobdup.ToList();
}

您可以像使用它一样使用

RemoveDupliacacse(list, new List<Func<ContactTemp, object>> {
   (ContactTemp contact) => contact.State, (ContactTemp contact) => contact.City })

您可能已经知道,当您选择多个列时,该方法会删除每列的重复项。请查看以下示例:

        var list = new List<ContactTemp> {
            new ContactTemp { City = "1", State = "1" },
            new ContactTemp { City = "1", State = "2" },
            new ContactTemp { City = "2", State = "1" },
            new ContactTemp { City = "2", State = "2" }
        };

        foreach (var contact in RemoveDupliacacse(
            list, 
            new List<Func<ContactTemp, object>> {
                (ContactTemp contact) => contact.State,
                (ContactTemp contact) => contact.City }))
        {
            Console.WriteLine($"City:{contact.City}, State:{contact.State}");
        }
        // This will output:
        // City: 1, State: 1

        // If you want to check duplication of the combination of the selected columns, 
        // you can do it like this;
        foreach (var contact in RemoveDupliacacse(
            list, 
            new List<Func<ContactTemp, object>> { 
                (ContactTemp contact) => new { contact.State, contact.City } }))
        {
            Console.WriteLine($"City:{contact.City}, State:{contact.State}");
        }
        // This will output:
        // City: 1, State: 1
        // City: 1, State: 2
        // City: 2, State: 1
        // City: 2, State: 2

答案 1 :(得分:0)

我认为你的班级喜欢

public class ContactTemp
{
  public string CITY{} 
  public int STATE{} 

}

此列表&#34; ContactTempList&#34;将有重复。您想要查找和删除此列表中CITY和STATE重复的项目。

我的意思是,这将为每个&#34;类型&#34;返回一个项目。 (如同一个区别)(所以如果你有A,A,B,C它会返回A,B,C)

List<ContactTemp> noDups = ContactTempList.GroupBy(d => new {d.CITY,d.STATE} )
                              .Select(d => d.First())
                              .ToList();

如果你只想要不重复的元素(如果你有A,A,B,C它将返回B,C):

List<ContactTemp> noDups = ContactTempList.GroupBy(d => new {d.CITY,d.STATE} )
                              .Where(d => d.Count() == 1)
                              .Select(d => d.First())
                              .ToList();

答案 2 :(得分:0)

您可以通过具有相同签名的反射来实现它,即任意数量的列:

public List<ContactTemp> RemoveDupliacacse(List<ContactTemp> ContactTempList, 
     List<string> objcolumn)
{            
    var type = typeof(ContactTemp);
    foreach (var column in objcolumn)
    {
        var property = type.GetProperty(column);
        ContactTempList = ContactTempList.GroupBy(x => property.GetValue(x))
                                         .Select(x => x.First()).ToList();
    }
    return ContactTempList;
}