Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 9th, 2008, 06:44 AM
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
Default 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..
__________________
DPK..
 
Old November 9th, 2008, 03:54 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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#.
 
Old November 9th, 2008, 08:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 1,093
Thanks: 1
Thanked 12 Times in 11 Posts
Default

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 November 11th, 2008, 03:56 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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.
 
Old November 11th, 2008, 09:10 AM
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
Default

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..
 
Old November 11th, 2008, 09:23 AM
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
Default

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..




Similar Threads
Thread Thread Starter Forum Replies Last Post
property umeshtheone Beginning VB 6 2 May 18th, 2007 12:37 AM
FileInfo Class Path Problem kwilliams VB.NET 6 April 18th, 2007 11:14 AM
Combining *.csv files sing FileInfo class mhite05 C# 0 October 25th, 2005 09:53 AM
Invalid use of property hanna VB.NET 2002/2003 Basics 1 March 31st, 2004 06:31 PM
FileInfo object bmains ASP.NET 1.x and 2.0 Application Design 3 January 14th, 2004 09:50 PM





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