Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

Finding most approximated item in collection object?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hyun-jik Bae - 24 Oct 2006 02:45 GMT
Is there any way how to get the item which has the most similar key in
key-value collection object such as Dictionary<> or SortedDictionary<>
although there is no matching key? If there is none, is there any
substitution class for enabling this?

Please reply. Thanks in advance.

Hyun-jik Bae
Champika Nirosh - 24 Oct 2006 04:39 GMT
to my understanding you are asking about a way to match the keys as such

key = "test"
if test is there then you should get the value of it and else you should get
the value of a key that has "testdata" I am I correct..

If yes then this is not possible and key has to match exactly. If you need
some thing like this then you may have to write a collection that does this
for you by probably exrednign the existing one.

Nirosh.

> Is there any way how to get the item which has the most similar key in
> key-value collection object such as Dictionary<> or SortedDictionary<>
[quoted text clipped - 4 lines]
>
> Hyun-jik Bae
KH - 24 Oct 2006 05:34 GMT
You're talking about some fuzzy logic there - first and most importantly you
need to define exactly what "most similar" means, between whatever objects
you're comparing. From there you would probably implement IComparer for the
type and a custom collection that would allow this fuzzy lookup.

For example for String, you could define "most similar" as the first match
less than or equal to a given value, then using a sorted set of values
(untested code here)...

string find = "d";
string[] values = { "a", "c", "e" };
string match = null;

Array.Sort(values);

foreach(string s in values)
{
   if (s.CompareTo(find) <= 0)
   {
       match = s;
       break;
   }
}

> Is there any way how to get the item which has the most similar key in
> key-value collection object such as Dictionary<> or SortedDictionary<>
[quoted text clipped - 4 lines]
>
> Hyun-jik Bae
Dave Sexton - 24 Oct 2006 05:38 GMT
Hi Hyun-jik,

You can subclass Dictionary or SortedDictionary and add your own property to
iterate the Keys collection and return an appropriate value, however you won't
be able to modify the indexer to match similar values.  If the hash codes
aren't consistent with the equality comparison then you won't be able to
locate existing keys in the Dictionary.

Signature

Dave Sexton

> Is there any way how to get the item which has the most similar key in
> key-value collection object such as Dictionary<> or SortedDictionary<>
[quoted text clipped - 4 lines]
>
> Hyun-jik Bae
Jon Shemitz - 24 Oct 2006 07:19 GMT

> Is there any way how to get the item which has the most similar key in
> key-value collection object such as Dictionary<> or SortedDictionary<>
> although there is no matching key? If there is none, is there any
> substitution class for enabling this?

Take a look at the Dictionary (IEqualityComparer<T>) constructor which
allows you to specify custom Equals and GetHashCode methods.

This may not be what you want, though. I get the impression (though I
may be wrong) that you only want to match, say, "Schmidt" if "Smith"
isn't found, while using an IEqualityComparer will always equate
"Schmidt" with "Smith."

Signature

.NET 2.0 for Delphi Programmers         www.midnightbeach.com/.net
Delphi skills make .NET easy to learn   Great reviews & good sales.

Michael C - 24 Oct 2006 08:11 GMT
> Is there any way how to get the item which has the most similar key in
> key-value collection object such as Dictionary<> or SortedDictionary<>
> although there is no matching key? If there is none, is there any
> substitution class for enabling this?

I don't see anyway except to iterate through the entire collection and apply
a comparison yourself. You'd have to rank items by similarity, sort them and
take the highest (or lowest) value.

> Please reply. Thanks in advance.
>
> Hyun-jik Bae
Ignacio Machin ( .NET/ C# MVP ) - 24 Oct 2006 13:03 GMT
Hi,

What is the problem that you have? maybe we can help you get a better
solution than the one you are expecting to use

> Is there any way how to get the item which has the most similar key in
> key-value collection object such as Dictionary<> or SortedDictionary<>
[quoted text clipped - 4 lines]
>
> Hyun-jik Bae

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.