p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 3.5 Basics (http://p2p.wrox.com/forumdisplay.php?f=351)
-   -   FileInfo Arrya Contains Property (http://p2p.wrox.com/showthread.php?t=71378)

dpkbahuguna November 9th, 2008 06:44 AM

FileInfo Arrya Contains Property
 
Hello friends,

     I have a FIleInfo collection and I want to search a particular file in this collection but I am unable to get the result...

     I am doing something like this please tell what I am doing wrong here my code is here below:

    In this code there are tow values are coming in FileInfo Ccollection and I have to search anyone according to condition

========================================
          var Qry = from x in FI where (x.ToString().ToLower()).StartsWith(fname.ToLower( )) select x;

        FileInfo RowTextFile = new FileInfo("FIleName.txt");

        FileInfo[] fileName = Qry.ToArray<FileInfo>();

//this FILENAME>TXT is available into FileInfo array so this IF CONTDITION should return TRUE value but condition returning FALSE.
        if(fileName.Contains<FileInfo>(RowTextFile))
    {
        dosomething;
    }
=================================

thanks in advance!!


DPK..

Old Pedant November 9th, 2008 03:54 PM

What language is this supposed to be????

You have var Qry, so it looks like JavaScript. Except it's not like any legal JavaScript that I have ever seen.

And then the rest of the code looks like C#.

Bob Bedell November 9th, 2008 08:32 PM

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:

Code:

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:

Code:

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

HTH,

Bob



Old Pedant November 11th, 2008 03:56 AM

Ahhh...makes eminent sense, now.

I think I also got hung up on the inefficiency of the SQL query. Doing toLowerCase() on a field in SQL pretty much guarantees that you can't get the best performance if the field in question is indexed. Since most SQL operations are case-insensitive, you'd be much better off with

var Qry = from x in FI where x LIKE fname+'%' select x;

Assuming that's legal LINQ syntax. But in any case, finding a way to use LIKE would presumably give better efficiency. Clearly wouldn't give worse.

dpkbahuguna November 11th, 2008 09:10 AM

Ya thanks to reply Old Pedant,

      using LINQ i have done it..but just wanted to know why is it not running as I was comparing it.

Thanks again.

Quote:

quote:Originally posted by Old Pedant
 Ahhh...makes eminent sense, now.

I think I also got hung up on the inefficiency of the SQL query. Doing toLowerCase() on a field in SQL pretty much guarantees that you can't get the best performance if the field in question is indexed. Since most SQL operations are case-insensitive, you'd be much better off with

var Qry = from x in FI where x LIKE fname+'%' select x;

Assuming that's legal LINQ syntax. But in any case, finding a way to use LIKE would presumably give better efficiency. Clearly wouldn't give worse.

DPK..

dpkbahuguna November 11th, 2008 09:23 AM

Thanks Bob Bedell,

   Nice explanation..thank you very much...

cheers :)!!


Quote:

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:

Code:

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:

Code:

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

HTH,

Bob



DPK..


All times are GMT -4. The time now is 01:48 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.