View Single Post
  #6 (permalink)  
Old November 11th, 2008, 09:23 AM
dpkbahuguna dpkbahuguna is offline
Friend of Wrox
Join Date: Jan 2006
Location: Hardwar, Uttranchal, India.
Posts: 180
Thanks: 1
Thanked 1 Time in 1 Post
Send a message via ICQ to dpkbahuguna Send a message via MSN to dpkbahuguna Send a message via Yahoo to dpkbahuguna

Thanks Bob Bedell,

   Nice explanation..thank you very much...

cheers :)!!

quote:Originally posted by Bob Bedell
 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))