title: Intersect method in LINQ with C#
body: <p>I am trying to obtain the same files using Intersect method from two folders.
  123.xml files are the same (no change with content, date, size) in all the folders.</p>
  
  <pre><code>Scores\Content\123.xml
  Scores\Content\hi.xml
  Scores\123.xml
  
  Power\Content\123.xml
  Power\Content\helo.xml
  Power\123.xml
  </code></pre>
  
  <p>This is from the C# code</p>
  
  <pre><code>        System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(path1);
          System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(path2);
  
          IEnumerable&lt;System.IO.FileInfo&gt; list1 = dir1.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
          IEnumerable&lt;System.IO.FileInfo&gt; list2 = dir2.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
  
          FileCompare myFileCompare = new FileCompare();
  
          bool areIdentical = list1.SequenceEqual(list2, myFileCompare);
  
          if (areIdentical == true)
          {
              Console.WriteLine("the two folders are the same");
          }
          else
          {
              Console.WriteLine("The two folders are not the same");
          }
  
  
          var queryCommonFiles = list1.Intersect(list2, myFileCompare);
  </code></pre>
  
  <p>The queryCommonFiles is only returning the 123.xml from Content folder, but not the other one.</p>
  
  <p>This is the code from the FileCompare</p>
  
  <pre><code>class FileCompare : System.Collections.Generic.IEqualityComparer&lt;System.IO.FileInfo&gt;
  {
  
      public FileCompare() { }
  
      public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
      {
          return (f1.Name == f2.Name &amp;&amp;
                  f1.Length == f2.Length);
      }
  
      // Return a hash that reflects the comparison criteria. According to the 
      // rules for IEqualityComparer&lt;T&gt;, if Equals is true, then the hash codes must
      // also be equal. Because equality as defined here is a simple value equality, not
      // reference identity, it is possible that two or more objects will produce the same
      // hash code.
      public int GetHashCode(System.IO.FileInfo fi)
      {
          string s = String.Format("{0}{1}", fi.Name, fi.Length);
          return s.GetHashCode();
      }
  
  }
  </code></pre>
  
  <p>EDIT:</p>
  
  <pre><code>var queryList1Only = (from file in list1
                                    select file).Except(list2, myFileCompare);
  
              Console.WriteLine("The following files are in list1 but not list2:\n");
              foreach (var v in queryList1Only)
              {
                  Console.WriteLine(v.FullName);
              }
  
  
              var queryList2Only = (from file in list2
                                    select file).Except(list1, myFileCompare);
  
              Console.WriteLine("The following files are in list2 but not list1:\n");
              foreach (var v in queryList2Only)
              {
                  Console.WriteLine(v.FullName);
              }
  </code></pre>
  
  <p>This produces as hi.xml for list1 and helo.xml for list2. And as I said only one 123.xml for the intersect method.</p>
  
  <p>Any suggestions would be appreciated</p>
  
  <p>Thanks,</p>