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

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

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

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

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

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

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
|
|
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 |
|
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |