Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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..
  #2 (permalink)  
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#.
  #3 (permalink)  
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


  #4 (permalink)  
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.
  #5 (permalink)  
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..
  #6 (permalink)  
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..
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:55 AM.


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