user5468794 user5468794 - 1 month ago 7
C# Question

Copy only distinct to second array

I am trying to copy an array to another one that only copies what is distinct. I had this working before I was doing a multi dim array. Now when I run the following code it just copies the original array to the second array. I also need to keep them in an array format as I output the data on a webpage later on. What am I doing wrong or can I not do this with multi dim?

string[][] array;
string[][] array2;
array2 = array.Distinct().ToArray();


After posting this can I edit my file.readalllines to not read lines that are the same? The code is below and table gets passed back as array.

string[][] table = File.ReadAllLines(@path)
.Select(line => line.Split(';'))
.ToArray();

Answer

For the sake of simplicity. Why not use this (apply distinct on line level):

 string[][] table = File.ReadAllLines(@path)
                   .Distinct()
                   .Select(line => line.Split(';'))
                   .ToArray();

The assumption is here, that equal lines lead to the same array. That means, the order is a component in the definition of equality of two different lines.

Comments