0

I have a list,in which every item is a list of stings themselves. How do I check whether all the individual items are same?

static void Main(string[] args)
{
    var myList = new List<List<string>>();
    var myItem1 = new List<string> { "str1", "str2" };
    var myItem2 = new List<string> { "str1", "str2" };

    myList.Add(myItem1);
    myList.Add(myItem2);

    var total = ??  // <- I'm stuck here

    Console.WriteLine(total);
    Console.ReadKey();
}

Now I want to check if every item inside myList are equal. I did try this one:Check if two list have the same items, but could not resolve.

Rufus L
  • 36,127
  • 5
  • 30
  • 43
user2948533
  • 1,143
  • 3
  • 13
  • 32
  • 2
    Does order of the items or duplicates matter? – Alexandru Puiu Oct 30 '18 at 23:43
  • 1
    I'm voting to close this question as off-topic because [simple google search would bring the answer to it](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.except?view=netframework-4.7.2#System_Linq_Enumerable_Except__1_System_Collections_Generic_IEnumerable___0__System_Collections_Generic_IEnumerable___0__). Tip: if this method returns no items - its equal – T.S. Oct 30 '18 at 23:45
  • When you say "could not resolve" when attempting your linked answer - what result did you get and what have you tried? – trailmax Oct 30 '18 at 23:49
  • @AlexandruPuiu, no does not matter. – user2948533 Oct 30 '18 at 23:51
  • @T.S., doing in simple foreach loop way is not what I was after.I was trying for an elegant LINQ solution. May be I should have added that.Sorry for the confusion. – user2948533 Oct 30 '18 at 23:53
  • Thanks for the update. It was not listed in the question – T.S. Oct 31 '18 at 00:01
  • 1
    So in your example, do you expect the result to be `false`, since `myList` contains both `"str1"` and `"str2"` (which are not equal)? – Rufus L Oct 31 '18 at 00:10
  • @RufusL, No I want it to be true since both the items in myList(myitem1 and myItem2) are containing same items.Basically I want to compare between myItem1 and myItem2 and if they contain same items,regardless of the order,it should return true.Also, please please note myList can contain many items-muItem1,2,3.. so on, wanna compare between them all. – user2948533 Oct 31 '18 at 00:32
  • Ok, I think that wasn't quite clear based on the answers you're getting. Next question is if `myItem1` had a duplicate entry, like `{ "str1", "str2", "str2" }`, should the equality comparison *still* return `true` or would it now be `false` since the count is different than `myItem2`? – Rufus L Oct 31 '18 at 00:35
  • that will not arise in my case as it will be sent as Disntict. – user2948533 Oct 31 '18 at 00:36
  • try var status = myItem1.SequenceEqual(myItem2); – Sooriya Dasanayake Oct 31 '18 at 04:11

2 Answers2

1

You could try something like this which should output true if the list's elements are all the same.

For your code:

if (myList.Any(item => !Enumerable.SequenceEqual(item, myList[0])))
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
AbsoluteSpace
  • 710
  • 2
  • 11
  • 21
0

To check if all the lists contain the exact same elements, we can use the SequenceEqual method, which compares both the elements themselves and their order. Since you said that order doesn't matter, however, then we must order the lists first when we do the comparison.

The following line of code returns true if All the lists (except the first one), when reduced to Distinct items and then sorted (OrderBy), are equal to the First list (when reduced to Distinct items and sorted):

bool allAreEqual = myList
    .Skip(1)
    .All(subList => subList
        .Distinct()
        .OrderBy(item => item)
        .SequenceEqual(myList.First().Distinct().OrderBy(item => item)));

Instead of doing it all in one line, it would be easier to read (in my opinion) and better performing to first get the distinct, ordered lists, and then do the comparison (otherwise we're ordering the first item for each of the other items):

var sortedLists = myList.Select(list => list.Distinct().OrderBy(item => item));
var allAreEqual = sortedLists.Skip(1).All(list => list.SequenceEqual(sortedLists.First()));
Rufus L
  • 36,127
  • 5
  • 30
  • 43