Accepted answer

You can group by a string produced from your list items taken in the same order. Assuming that '|' character is not allowed inside names, you can do this:

var namelist = json_converted
    .GroupBy(n => string.Join("|", n.names.OrderBy(s => s)))
    .Select(g => new {
        Name = g.First().names
    ,   Count = g.Count()

This approach constructs a string "a|b" from lists ["a", "b"] and ["b", "a"], and use that string for grouping the content.

Since the keys are composed of ordered names, g.First().names used as Name may not be in the same order for all elements of the group.


You can write a comparer for the list of names.

public class NamesComparer : IEqualityComparer<IEnumerable<string>>
    public bool Equals(IEnumerable<string> x, IEnumerable<string> y)
        //do your null checks
        return x.SequenceEqual(y);

    public int GetHashCode(IEnumerable<string> obj)
        return 0;

Then use that in GroupBy:

var namelist = json_converted.GroupBy(n => n.names, new NamesComparer())
                             .Select(i => new { name = i.Key, Count = i.Count() });

Related Articles