c# - How can I compare two unordered sequences (list and array) for equality? -
i have string array string str[] = {"a", "b"}
and list<string> lst = new list<string> {"a", "b"}
how can make sure both string array , list contains same values. note: values can in order must have same frequency.
can tell me how in linq?
thanks.
okay, since order not matter frequncies do, need count each key, , check resulting pairs of keys/counts equal:
var first = str.groupby(s => s) .todictionary(g => g.key, g => g.count()); var second = lst.groupby(s => s) .todictionary(g => g.key, g => g.count()); bool equals = first.orderby(kvp => kvp.key) .sequenceequals(second.orderby(kvp => kvp.key));
Comments
Post a Comment