C# 从二维数组中删除重复行

C# 从二维数组中删除重复行,c#,multidimensional-array,row,C#,Multidimensional Array,Row,假设我有一个表示简单矩阵的二维数组 int[,] matrix= new int[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } }; 看起来是这样的 1 2 3 4 1 2 7 8 是否有任何方法可以使用LINQ删除重复行并使数组看起来像这样 1 2 3 4 7 8 int[,]list=newint[,]{{1,2},{3,4},{1,2},{7,8}; List newList=新列表(); 布尔·杜普发现; for(int i=0;i

假设我有一个表示简单矩阵的二维数组

int[,] matrix= new int[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } };
看起来是这样的

1 2
3 4
1 2
7 8
是否有任何方法可以使用LINQ删除重复行并使数组看起来像这样

1 2
3 4
7 8
int[,]list=newint[,]{{1,2},{3,4},{1,2},{7,8};
List newList=新列表();
布尔·杜普发现;
for(int i=0;i
这不是真正的Linq,但您可以定义一些帮助器方法,就像它们是Linq方法一样

较简单的算法应为:

  • 转换为列表的列表
  • 使用自定义比较器应用不同的
  • 重建另一个阵列
  • 这看起来像这样:

    public static class MyExtensions
    {
        public static IEnumerable<List<T>> ToEnumerableOfEnumerable<T>(this T[,] array)
        {
            int rowCount = array.GetLength(0);
            int columnCount = array.GetLength(1);
    
            for (int rowIndex = 0; rowIndex < rowCount; rowIndex++)
            {
                var row = new List<T>();
                for (int columnIndex = 0; columnIndex < columnCount; columnIndex++)
                {
                    row.Add(array[rowIndex, columnIndex]);
                }
                yield return row;
            }
        }
        public static T[,] ToTwoDimensionalArray<T>(this List<List<T>> tuples)
        {
            var list = tuples.ToList();
            T[,] array = null;
            for (int rowIndex = 0; rowIndex < list.Count; rowIndex++)
            {
                var row = list[rowIndex];
                if (array == null)
                {
                    array = new T[list.Count, row.Count];
                }
                for (int columnIndex = 0; columnIndex < row.Count; columnIndex++)
                {
                    array[rowIndex, columnIndex] = row[columnIndex];
                }
            }
            return array;
        }
    }
    
    公共静态类MyExtensions
    {
    公共静态IEnumerable到numerableofEnumerable(此T[,]数组)
    {
    int rowCount=array.GetLength(0);
    int columnCount=array.GetLength(1);
    对于(int rowIndex=0;rowIndex
    自定义列表比较器:

    公共类ListQualityComparer:IEqualityComparer
    {
    公共布尔等于(列表x、列表y)
    {
    返回x.x(y);
    }
    public int GetHashCode(列表对象)
    {
    int hash=19;
    foreach(obj中的var o)
    {
    hash=hash*31+o.GetHashCode();
    }
    返回散列;
    }
    }
    
    用法:

    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var array = new[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } };
            array = array.ToEnumerableOfEnumerable()
                         .Distinct(new ListEqualityComparer<int>())
                         .ToList()
                         .ToTwoDimensionalArray();
        }
    }
    
    [TestClass]
    公共类UnitTest1
    {
    [测试方法]
    公共void TestMethod1()
    {
    var数组=新[,]{1,2},{3,4},{1,2},{7,8};
    array=array.ToEnumerableOfEnumerable()的
    .Distinct(新的ListQualityComparer())
    托利斯先生()
    .totwo维度数组();
    }
    }
    
    阵列的大小是静态的。您不能删除ítems(但您可以创建一个新的数组)。+1如果要使用linq,您需要更容易过滤的锯齿状数组。因此,需要将其转换为锯齿形。这就像一个符咒!非常感谢你的帮助。
    public class ListEqualityComparer<T> : IEqualityComparer<List<T>>
    {
        public bool Equals(List<T> x, List<T> y)
        {
            return x.SequenceEqual(y);
        }
    
        public int GetHashCode(List<T> obj)
        {
            int hash = 19;
            foreach (var o in obj)
            {
                hash = hash * 31 + o.GetHashCode();
            }
            return hash;
        }
    }
    
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var array = new[,] { { 1, 2 }, { 3, 4 }, { 1, 2 }, { 7, 8 } };
            array = array.ToEnumerableOfEnumerable()
                         .Distinct(new ListEqualityComparer<int>())
                         .ToList()
                         .ToTwoDimensionalArray();
        }
    }