0

I am trying to compare 2 collection with lists of strings. 1st object with list of strings:

a0, b0, c0

a1, b1, c0

a1, b2, c0

a1, b2, c1

2nd

a0, b0, c0

a1, b2, c0

I have method go get list of string from object

public List<string> GetPlainRow(int index)
{
    if(index >= _countRows || index < 0)
    {
        throw new IndexOutOfRangeException();
    }

    List<string> returnedRow = new List<string>();

    foreach (KeyValuePair<string, List<string>> entry in _dataRows)
    {
        returnedRow.Add(entry.Value[index]);
    }
    return returnedRow;
}

Tried to compare objects lists with:

for (int i = 0; i < dataCollection.CountRows; i++)
{
    var newRow = table.NewRow();
    for (var j = 0; j < dataCollection.GetPlainRow(i).Count; j++)
    {
        newRow[j] = dataCollection.GetPlainRow(i)[j];

    }
    for (int k = 0; k < dataCollection.CountRows; k++)
    {
        for (int l = 0; l < dataRestrained.CountRows; l++)
        {
           if(dataCollection.GetPlainRow(k).SequenceEqual(dataRestrained.GetPlainRow(l)))
            {
                found = true;
            }
        }
    }
    newRow[dataCollection.CountRows - 2] = found;
    found = false;
    table.Rows.Add(newRow);
}

But it seems like everytime its returning true, but when i've used Equals instead of SequenceEqual it was returning false.

Dicanio
  • 3
  • 6
  • 1
    Take a moment to read through the [editing help](//stackoverflow.com/editing-help) in the help center. Formatting on Stack Overflow is different than on other sites. The better your post looks, the easier it is for others to read and understand it. – gunr2171 Nov 05 '19 at 18:26
  • 3
    `Equals` for two lists will only return true if the two list variables reference the same instance, not if they contain the same data. `SequenceEqual` will actually check the data inside of the lists. – juharr Nov 05 '19 at 18:28
  • 3
    Possible duplicate of [How to compare two List to each other?](https://stackoverflow.com/questions/9602579/how-to-compare-two-liststring-to-each-other) –  Nov 05 '19 at 18:33
  • 1
    I'm having trouble understanding the desired output. If the input is two `List>` variables described above, what is the output? – Tom Faltesek Nov 05 '19 at 18:43
  • What do you mean by "compare" in this context? – NetMage Nov 05 '19 at 18:56
  • Im trying to create DataTable for DataGrid, DataTable. DataTable is basically 1st list of lists of strings, but there is additional column with true/false. Wanted to make true if certain 2nd list element is on 1st list, and false if not – Dicanio Nov 05 '19 at 18:58

2 Answers2

0

to make thing simple i add a class for the result:

   internal class Result
            {
                public List<string> List { get; set; }
                public bool IsExisted { get; set; }
            }

then i have made an EqualityComparer ....

  internal class ListOfStringEqualityComparer : IEqualityComparer<List<string>>
        {
            public bool Equals(List<string> b1, List<string> b2)
            {
                if (b1.Count != b2.Count) return false;
                for (int i = 0; i < b1.Count; i++)
                {
                    if (b1[i] != b2[i]) return false;
                }
                return true;
            }

            public int GetHashCode(List<string> b2)
            {
                int hCode = 0;
                for (int i = 0; i < b2.Count; i++)
                {
                    hCode = EqualityComparer<string>.Default.GetHashCode(b2[i]);
                }
                return hCode.GetHashCode();
            }
        }

now you can try this:

 ListOfStringEqualityComparer  listOfStringEqualityComparer= new ListOfStringEqualityComparer();
        var q = (from c in bigList
                 select new Result() { List = new List<string>(c), IsExisted = smallList.Contains(c, listOfStringEqualityComparer) });

let me now if it help ...

OMR
  • 11,736
  • 5
  • 20
  • 35
  • Just forget about list of lists of strings. Lets take it as list of strings. I have couple of lists of strings, lets say they are 'A', and i have couple of lists of strings - 'B'. 'A' has every list which is in 'B'. DataGrid is based on 'A' with extra column with true/false. I wanted to compare A with B. If 'A' element being compared at the moment is in 'B' column for this list will be true, if its not in 'B' it will be false – Dicanio Nov 05 '19 at 19:09
0

Maybe something like this

class Someclass
{
  string data;
  bool found;
}

Someclass data = A.Select(c => new Someclass(){data = c, found = B.Contains(c)});

Nafis Islam
  • 1,483
  • 1
  • 14
  • 34