如何使用LINQ </string>从List <string>中删除重复的组合

时间:2010-09-21 06:33:23

标签: c# linq duplicate-removal

我有一个像

这样的字符串列表
List<string> MyList = new List<string>
{ 
    "A-B", 
    "B-A", 
    "C-D", 
    "C-E", 
    "D-C",
    "D-E",
    "E-C",
    "E-D",
    "F-G",
    "G-F"
};

我需要删除列表中的副本,即如果存在“A-B”和“B-A”,那么我只需要保留“A-B”(第一个条目)

所以结果就像

"A-B"   
"C-D"
"C-E"   
"D-E"
"F-G"

有没有办法用LINQ做到这一点?

6 个答案:

答案 0 :(得分:14)

实现IEqualityComparer会在Equals(“A-B”,“B-A”)上返回true。并使用Enumerable.Distinct方法

答案 1 :(得分:12)

这将返回您查找的序列:

var result = MyList
    .Select(s => s.Split('-').OrderBy(s1 => s1))
    .Select(a => string.Join("-", a.ToArray()))
    .Distinct();

foreach (var str in result)
{
    Console.WriteLine(str);
}

简而言之:将-字符上的每个字符串拆分为两个元素的数组。对每个数组进行排序,然后将它们连接在一起然后,您只需使用Distinct即可获得唯一值。

更新:在考虑更多内容时,我意识到您可以轻松删除其中一个Select来电:

var result = MyList
    .Select(s => string.Join("-", s.Split('-').OrderBy(s1 => s1).ToArray()))
    .Distinct();

免责声明:此解决方案将始终将值“A-B”保持在“B-A”之上,而不管原始序列中出现的顺序如何。

答案 2 :(得分:4)

您可以使用Enumerable.Distinct(IEnumerable<TSource>, IEqualityComparer<TSource>)重载。

现在您只需要实施IEqualityComparer。这是你开始的事情:

class Comparer : IEqualityComparer<String>
{

    public bool Equals(String s1, String s2)
    {
        // will need to test for nullity
        return Reverse(s1).Equals(s2);
    }

    public int GetHashCode(String s)
    {
        // will have to implement this
    }

}

对于Reverse()实施,请参阅this question

答案 3 :(得分:1)

非常基本,但写得更好(但它只是工作):

class Comparer : IEqualityComparer<string>
  {
      public bool Equals(string x, string y)
      {
          return (x[0] == y[0] && x[2] == y[2]) || (x[0] == y[2] && x[2] == y[0]);
      }

      public int GetHashCode(string obj)
      {
          return 0;
      }
  }

var MyList = new List<String>
{ 
    "A-B", 
    "B-A", 
    "C-D", 
    "C-E", 
    "D-C",
    "D-E",
    "E-C",
    "E-D",
    "F-G",
    "G-F"
}
.Distinct(new Comparer());

foreach (var s in MyList)
{
    Console.WriteLine(s);
}

答案 4 :(得分:1)

您需要像这样实现IEqualityComparer:

public class CharComparer : IEqualityComparer<string>
{
    #region IEqualityComparer<string> Members

    public bool Equals(string x, string y)
    {
        if (x == y)
            return true;

        if (x.Length == 3 && y.Length == 3)
        {
            if (x[2] == y[0] && x[0] == y[2])
                return true;

            if (x[0] == y[2] && x[2] == y[0])
                return true;
        }

        return false;
    }

    public int GetHashCode(string obj)
    {
        // return 0 to force the Equals to fire (otherwise it won't...!)
        return 0;
    }

    #endregion
}

示例程序:

class Program
{
    static void Main(string[] args)
    {
        List<string> MyList = new List<string>
        { 
            "A-B", 
            "B-A", 
            "C-D", 
            "C-E", 
            "D-C",
            "D-E",
            "E-C",
            "E-D",
            "F-G",
            "G-F"
        };

        var distinct = MyList.Distinct(new CharComparer());
        foreach (string s in distinct)
            Console.WriteLine(s);

        Console.ReadLine();
    }
}

结果:

"A-B"   
"C-D"
"C-E"   
"D-E"
"F-G"

答案 5 :(得分:-2)

int checkID = 0;
while (checkID < MyList.Count)
{
 string szCheckItem = MyList[checkID];
 string []Pairs = szCheckItem.Split("-".ToCharArray());
 string szInvertItem = Pairs[1] + "-" + Pairs[0];
 int i=checkID+1;
 while (i < MyList.Count)
 {
  if((MyList[i] == szCheckItem) || (MyList[i] == szInvertItem))
  {
   MyList.RemoveAt(i);
   continue;
  }
  i++;
 }

 checkID++;
}