View Single Post
  #3 (permalink)  
Old November 9th, 2008, 08:32 PM
Bob Bedell Bob Bedell is offline
Friend of Wrox
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts

The language is C# 3.0 with implicitly typed local variable and LINQ query operations support.

The problem is that, since Object.Equals isn't overriden in your FileInfo instances, your comparison is using normal reference type equality semantics. The comparison instances aren't the same object, so the comparison returns false.

Here's is a little custom equality comparer that will allow you to compare you FileInfo object's Name property:

public class FileNameComparer : EqualityComparer<FileInfo>
        public override bool Equals(FileInfo x, FileInfo y)
            return x.Name == y.Name;

        public override int GetHashCode(FileInfo obj)
            return (obj.Name).GetHashCode();
Now in your code, instantiate a FileNameComparer, then use the Enumerable.Contains<TSource> overload that accepts two parameters: an IEnumerable<TSource>, and a IEqualityComparer<TSource>, like:

FileInfo[] fileName = Qry.ToArray<FileInfo>();
            FileNameComparer fileNameComparer = new FileNameComparer();
            if (fileName.Contains<FileInfo>(RowTextFile, fileNameComparer))