3

我有一个像这样的字符串列表

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"
};

我需要从列表中删除重复项,即如果“AB”和“BA”存在,那么我只需要保留“AB”(第一个条目)

所以结果会像

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

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

4

6 回答 6

14

实现 IEqualityComparer 女巫在 Equals("AB", "BA") 上返回 true。并使用Enumerable.Distinct方法

于 2010-09-21T06:39:31.953 回答
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();

免责声明:此解决方案将始终保持值“AB”超过“BA”,无论原始序列中出现的顺序如何。

于 2010-09-21T06:44:04.677 回答
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()实现,请参阅此问题

于 2010-09-21T06:40:45.773 回答
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);
}
于 2010-09-21T06:47:25.710 回答
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();
    }
}

结果:

“AB”   
“光盘”
“行政长官”   
“德”
“FG”
于 2010-09-21T07:07:09.060 回答
-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++;
}
于 2010-09-21T06:48:00.400 回答