Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>There's a useful LINQ operator called <a href="http://msdn.microsoft.com/en-us/library/bb495305.aspx" rel="nofollow"><code>SequenceEqual()</code></a> which compares two sequences for equality.</strong> <code>SequenceEqual()</code> walks through any two <code>IEnumerable&lt;&gt;</code> sequences and verifies that they have the same number of elements and that elements at the same index are equal (using the default equality comparer). However, since you have nested collections, you need to extend the concept of equality to apply to them as well. Fortunately, there's an overload that allows you supply your own <code>IEqualityComparer&lt;&gt;</code> object. </p> <p>Since it's awkward to constantly have to define a class to provide equality semantics, I've written a generic extension that allows you to use a delegate instead. Let's look at the code:</p> <pre><code>public static class ComparerExt { private class GenericComparer&lt;T&gt; : IEqualityComparer&lt;T&gt; { private readonly Func&lt;T, T, bool&gt; m_EqualityFunc; public GenericComparer( Func&lt;T,T,bool&gt; compareFunc ) { m_EqualityFunc = compareFunc; } public bool Equals(T x, T y) { return m_EqualityFunc(x, y); } } // converts a delegate into an IComparer public static IEqualityComparer&lt;T&gt; AreEqual&lt;T&gt;( Func&lt;T,T,bool&gt; compareFunc ) { compareFunc.ThrowIfNull("compareFunc"); return new GenericComparer&lt;T&gt;(compareFunc); } } </code></pre> <p><strong>Now, we can compare two sequences easily enough:</strong></p> <pre><code>Assert.IsTrue( // check that outer sequences are equivalent... a1.SequenceEqual( a2, // define equality as inner sequences being equal... ComparerExt.AreEqual( (a,b) =&gt; a.SequenceEqual(b) ) ); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload