Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional 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 September 3rd, 2007, 10:39 AM
Friend of Wrox
 
Join Date: Mar 2006
Location: , , Portugal.
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts
Default list.exists

I am going crazy with lists statements...

I have a list(of T) = mylist(of category)

My category is:
category.ID
category.Name
category.ParentID

I need to a search if that list contains any category with "parentID" equal to the currentID that i Have

I try to do something

mylist.Exists(....(any category with parentID = x))

But don't had succeed with all my tryings..
I had spend all night searching for this without success

I know search values in datatable by rows values...
But how can i do it with list(of T) ?

Thanks very much

  #2 (permalink)  
Old September 3rd, 2007, 11:02 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

hi there..

did you read this:
http://msdn2.microsoft.com/en-us/lib...ca(vs.80).aspx

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
  #3 (permalink)  
Old September 3rd, 2007, 11:44 AM
Friend of Wrox
 
Join Date: Mar 2006
Location: , , Portugal.
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes...

But this example is for an unidimensional list:

>List(Of String)

This example show how can we count any occurrence of an item!

But in my case I need to check an occurrence of an item that have "parentID" equal to xxx (for example)

  #4 (permalink)  
Old September 3rd, 2007, 11:48 AM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

yes. but you have to change the method to do the search in the type of objects you are using.. in your case you will read every category object until you find the one you want...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
  #5 (permalink)  
Old September 3rd, 2007, 12:07 PM
Friend of Wrox
 
Join Date: Mar 2006
Location: , , Portugal.
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello!

I already read every value... But if it is the only way i need to do 2 reads :(
Code:
Private Sub PopulateTVNodes(ByVal li As List(Of BLL.Articles.Category), _
      ByVal nodes As TreeNodeCollection)

            For Each categ As BLL.Articles.Category In li
                Dim tn As New TreeNode()

                If categ.ParentID = 0 Then
                    tn.Text = categ.Name
                    tn.Value = categ.ID
                    nodes.Add(tn)

                    tn.PopulateOnDemand = li.Exists(...If any item has the currentParentID on his parentID run populate...)

                End If
End Sub
  #6 (permalink)  
Old September 3rd, 2007, 12:11 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

and how did you code the exists delegate??

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
  #7 (permalink)  
Old September 3rd, 2007, 02:19 PM
Friend of Wrox
 
Join Date: Mar 2006
Location: , , Portugal.
Posts: 310
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by Maxxim
 
Quote:
quote:Originally posted by gbianchi
Quote:
 and how did you code the exists delegate??
It's that I want to know... :(

I will use a worst solution:

Code:
Private Sub PopulateTVNodes(ByVal li As List(Of BLL.Articles.Category), _
      ByVal nodes As TreeNodeCollection)

            For Each categ As BLL.Articles.Category In li
                Dim tn As New TreeNode()

                If categ.ParentID = 0 Then
                    tn.Text = categ.Name
                    tn.Value = categ.ID
                    nodes.Add(tn)

                    'tn.PopulateOnDemand = li.Exists(...If any item has the currentParentID on his parentID run populate...)
                    For Each Allcateg As BLL.Articles.Category In li
                        If Allcateg.ParentID = categ.ID Then
                            tn.PopulateOnDemand = True
                            Exit For
                        End If
                    Next


                End If
        Next
End Sub


  #8 (permalink)  
Old September 3rd, 2007, 02:23 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

well.. that's not the worst solution.. that's "the solution"... maybe if your data is not too much you can try using a dictionary??? that way it will have a way to look out for values (e.g. you could us the id as the key)...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
  #9 (permalink)  
Old September 3rd, 2007, 02:46 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Yeah, I agree. It's not that bad. Find, or Exist, or Contains basically carry out the same looping code anyway.

Doug's suggestion for another tracking collection like a dictionary may speed things up a little.

If you were using C#, you could use anonymous delegates to accomplish the same, but with less code. I realize the following is of no use to you because you write in VB, but it's an interesting solution (IMO) nonetheless:
Code:
public static bool DemoMethod()
{
  List<Category> myList = new List<Category>();
  myList.Add(new Category() { ParentId = 10, Id = 1 });
  myList.Add(new Category() { ParentId = 14, Id = 2 });
  myList.Add(new Category() { ParentId = 12, Id = 3 });

  int parentId = 14;
  return myList.Exists(delegate(Category myCategory) { return myCategory.ParentId == parentId; });
}
The code uses C# 3 object initializers to quickly fill the list, but you can of course replace that with another way to fill the list (I just needed some sample data). Additionally, it uses a hardcoded value for the parentId, but it gets the job done. The anonymous delegate is like an inline method. You send it an instance of Category which you can use to compare its ParentId to the parentId variable in the method body.

Again, a useless reply in your situation, but it may help someone else who needs a C# version in the future.

Cheers, and sorry for the off-topic reply..... ;)

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
  #10 (permalink)  
Old September 3rd, 2007, 02:52 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Quote:
quote:Originally posted by Imar
 Doug's suggestion for another tracking collection like a dictionary may speed things up a little.


I'm Doug?? ;) or there is a post that I didn't see?? :D

And since when your interventions are useless??? you are always welcome...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 


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
"A command with that name already exists" Aaron Edwards Visual Studio 2005 2 March 9th, 2007 03:45 PM
File already exists srsandhu ASP.NET 1.0 and 1.1 Basics 1 February 14th, 2007 08:43 PM
table already exists mussa MySQL 1 July 13th, 2006 03:01 AM
'exists' dilemma defiant SQL Server 2000 4 June 27th, 2003 07:26 AM



All times are GMT -4. The time now is 11:18 AM.


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