Using SequenceEqual and then returning which elements don't match

I have two List<string> and I am using the SequenceEqual method to determine if they match.

I now have a need to get a List<int>, string, IEnumerable, whatever of the indexes of the elements that have failed.

Is there an easy way?

Thanks

Answers


I think you want:

List<string> list1 = ...
List<string> list2 = ...

var differentIndices = list1.Zip(list2, (item1, item2) => item1 == item2)
                            .Select((match, index) => new { Match = match, Index = index })
                            .Where(a => !a.Match)
                            .Select(a => a.Index);

Note that if one of the lists is longer than the other, this will not consider the items beyond the length of the smaller list.


Need Your Help

Redirect to a JSP file from controller action in Spring MVC

java spring jsp spring-mvc

In my spring mvc application, I have the following login form: