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 / May 2007

Tip: Looking for answers? Try searching our database.

Finding an object property in list

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mpaine - 25 May 2007 05:52 GMT
Hello,

I am trying to avoid using a DataTable to find particular data rows, because
I believe generic.list(of someobject) is faster.  Unfortunately, two
seperately instantiated objects do not have the same hashcode so I am unable
to compare them within a Contains() call [even though they contain the same
data].   I know this is what primary keys are for but I wanted to know if
there is a way to use Contains() on a list object, such as:

   Public Class SentenceContent
       Public Sentence As String
       Public Score As Integer
       Public Sub New(ByVal Sentence As String, ByVal Score As Integer)
           Me.Sentence = Sentence
           Me.Score = Score
       End Sub
   End Class    
   Public Sentences As New Generic.List(Of SentenceContent)

       Sentences.Add(New SentenceContent("testing", 1))
       Sentences.Add(New SentenceContent("testing 1", 2))
       Sentences.Add(New SentenceContent("testing 2", 3))
       Sentences.Add(New SentenceContent("testing 3", 4))
       Sentences.Add(New SentenceContent("testing 4", 5))
       Sentences.Add(New SentenceContent("testing 5", 6))
       Sentences.Add(New SentenceContent("testing 6", 7))
       If Sentences.Contains(New SentenceContent("testing 5", 6)) Then
           test.Text = "Contains" & "<br/>"
       Else
           test.Text = "NOT Contains" & "<br/>"
       End If

I'm sure this is old news to you but I would very much appreciate your input
here.

Thanks!

mpaine
Signature

msdn premium subscriber

Moty Michaely - 25 May 2007 06:34 GMT
> Hello,
>
[quoted text clipped - 36 lines]
> --
> msdn premium subscriber

Dear mpaine,

Yes, you can do that by the Find method of the generic list, and a
specific predicate.

The Find method searches for an element that matches the conditions
defined by the specified predicate.
The Predicate is a delegate to a method that returns true if the
object passed to it matches the conditions defined in the delegate.The
elements of the current List are individually passed to the Predicate
delegate, moving forward in the List, starting with the first element
and ending with the last element. Processing is stopped when a match
is found.

Since the search is linear it's an O(n) operation where n is the
number of elements in the list.

If you need faster search capabilities, try using other data
structures (such as Hashtable (O(1)) - to solve the hashcode problem,
just derive from the class that would be the key and make sure to
return the same hashcode for objects that are equal, or Dictionary
(O(1)) with specified comparer).

Hope this helps,
Moty.
Linda Liu [MSFT] - 25 May 2007 08:30 GMT
Hi Mpaine,

List(Of T) doesn't support the function you want. The reason is as what you
have said--different object instances have different hash code.

Dictionary(Of TKey, TValue) should meet your requirement.

I thought a possible workaround is to derive a new type from List<T>  and
override the Contains method. Unfortunately, the Contains method isn't
overridable. So it seems it is impossible to get the List(Of T) to support
the function you want.

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
Moty Michaely - 25 May 2007 09:46 GMT
On May 25, 10:30 am, v-l...@online.microsoft.com (Linda Liu [MSFT])
wrote:
> Hi Mpaine,
>
[quoted text clipped - 32 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights.

Dear Linda,

The List(of T) does contain a method that determines whether the List
contains elements that match specified conditions.
Methods like Exists or Find can suite for the OP needs.

All he has to do is implement a delegate (anonymous delegate would be
great for this purpose) to be used as a predicate (equality comparer).

Contains method is the same as exists. It just determines equality
using the default equality comparer EqualityComparer.Default for T,
the type of values in the list.

To match his needs he just have to call exists with his type of
equality comparer.

Moty
mpaine - 25 May 2007 18:48 GMT
This is all helpful, thank you everyone!

Signature

msdn premium subscriber

> Hello,
>
[quoted text clipped - 34 lines]
>
> mpaine
mpaine - 27 May 2007 07:44 GMT
just as a friendly followup, I found the easiest solution for my needs -- use
structures instead of classes, they automatically compare the internal data
for equals on their hashcodes.
Signature

msdn premium subscriber

> Hello,
>
[quoted text clipped - 34 lines]
>
> mpaine
Linda Liu [MSFT] - 28 May 2007 10:28 GMT
Hi Mpaine,

Thank you for sharing your solution with us.

Structure is a value type. When two value type objects are compared, the
values contained in the two objects are compared actually.

But note that a struct is considered a lightweight class, ideal for
creating data types that store small amounts of data, and does not
represent a type that might later be extended via inheritance.

BTW, I'd like to thank Moty for pointing out the Exists and Find method of
the List(Of T) class. I think it is a good solution to this problem.

If you have any other question in the future, please don't hesitate to
contact us. It's always our pleasure to be of assistance!

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support
Moty Michaely - 29 May 2007 08:41 GMT
On May 28, 12:28 pm, v-l...@online.microsoft.com (Linda Liu [MSFT])
wrote:
> Hi Mpaine,
>
[quoted text clipped - 18 lines]
> Linda Liu
> Microsoft Online Community Support

Hi,

Linda is right about structures. It should not be used if it's
intended to extension in the future.

I would override the IComparable interface for equality purposes.

Linda - Glad I could point that out.

Have a great day,
Moty

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.