There is a similar question that doesn't answer my question. --> Count number of element in List>
I have a list which contains sublists:
List<string> sublist1 = new List<string>() { "a", "b" };
List<string> sublist2 = new List<string>() { "a", "b" };
List<string> sublist3 = new List<string>() { "a", "c" };
Now I want to count the occurrences of each list.
a, b --> 2
a, c --> 1
I used distinct()
from LINQ, but I got the output:
a, b --> 1
a, b --> 1
a, c --> 1
I assume that the hashcode is different.
Is there an alternative to distinct()
which is looking at the list values instead?
I want to solve this in LINQ if possible.
Edit: The order of list items has to be the same!
>`) - however, I think you should use my version simply because I added null checking to it - the other answer will fail if there are any null strings in the lists. (Technically the other implementation could also fail if checking for arithmetic overflow is enabled, whereas my implementation explicity uses `unchecked` to avoid that issue.)
– Matthew Watson Nov 12 '19 at 11:01