question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal: IsEquivalentTo matcher for IEnumerables ignoring order

See original GitHub issue

Hey, I would like to propose a matcher similar to IsSameSequenceAs, but one that ignores ordering. Similar to CollectionAssert.AreEquivalent().

My initial idea was to implement a new IsEquivalentTo method that would have a required OrderByKey parameter which would be used to order both enumerables and then call IsSameSequenceAs internally for the matching.

Would like to know your opinions on it and if I should work on it.

Thanks in advance.

Issue Analytics

  • State:open
  • Created 7 months ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
thomaslevesquecommented, Mar 2, 2023

Yeah, I guess in typical unit tests you don’t work on very large lists… And what I said about using EqualityComparer<T>.Default doesn’t apply, unfortunately, because we won’t have a T (well, we will, but it will be the type of the collection, not the type of the items). So we’ll just have to use EqualityComparer<object>.Default instead.

0reactions
blairconradcommented, Mar 2, 2023

We should just use the default equality comparer for the type (EqualityComparer<T>.Default) … And of course, we can let the user specify an equality comparer explicitly.

👍

I would probably go with an method that runs a match such as first.Count() == second.Count() && first.All(second.Contains)

That would be pretty inefficient (O(m*n))

I don’t think the efficiency is a big deal. Or at least we could be inefficient at first and see if it becomes one. I’m not against a more efficient solution, but if we don’t become convinced there’s a working and efficient solution, working inefficient is a reasonable tradeoff, IMO.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Compare two List<T> objects for equality, ignoring order
I need to check that they both have the same elements, regardless of their position within the list. Each MyType object may appear...
Read more >
HashSets are wrongly compared as IEnumerables where ...
cs where we try to match the individual elements, and this matching is done using CollectionTally.ItemsEqual(object expected, object actual) .
Read more >
C# | Jon Skeet's coding blog | Page 11
It allows you to order any sequence implementing IEnumerable<T> – rather than Array.Sort being separate from List<T>.Sort, for example.
Read more >
High Performance PowerShell with LINQ - Simple Talk
Casts the elements of an IEnumerable to the specified type, effectively converting IEnumerable to IEnumerable<T> , which then makes the sequence ...
Read more >
4. Advanced C# - C# 10 in a Nutshell [Book]
Our Transform method is a higher-order function because it’s a function that takes a function as an argument. (A method that returns a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found