How to compare Lists of Guids in C#

I have got this structure

var values = new Dictionary<int, List<Guid>>();

And I have to say if all dictionary elements has the same set of List<Guid>.

I dont need to know which are exactly are different, just to answer the question.

So it looks like

List A { 1, 2, 3} List B { 1, 2, 3} List C { 1, 2, 3} the same and have no difference.

and

List A { 3, 2, 3} List B { 1, 2, 3} List C { 1, 2, 3} are not the same.

I have no clue where I can start it.

Initially i guessed to convert List<Guid> to string and just do distinct operation over it.

But is this a good approach?

Thank you!

Jon Skeet
people
quotationmark

I'd create a HashSet<Guid> from one of the values (any) and then check that all of the others are equal to it:

// TODO: Handle the dictionary being empty
var firstSet = new HashSet<Guid>(values.First().Value);
var allEqual = values.All(pair => firstSet.SetEquals(pair.Value));

This assumes that:

  • The order within each list is unimportant
  • The number of times each GUID appears in the list is unimportant

(i.e. you really are thinking of them as sets, not lists, at least for this part of the code)

In other words, if you have guids A and B, the code above assumes that { A, B, B } is equivalent to { B, A }.

people

See more on this question at Stackoverflow