Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 3rd, 2007, 10:39 AM
Friend of Wrox
 
Join Date: Mar 2006
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

 
Old September 3rd, 2007, 11:02 AM
Friend of Wrox
 
Join Date: Jun 2003
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
================================================== =========
 
Old September 3rd, 2007, 11:44 AM
Friend of Wrox
 
Join Date: Mar 2006
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)

 
Old September 3rd, 2007, 11:48 AM
Friend of Wrox
 
Join Date: Jun 2003
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
================================================== =========
 
Old September 3rd, 2007, 12:07 PM
Friend of Wrox
 
Join Date: Mar 2006
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
 
Old September 3rd, 2007, 12:11 PM
Friend of Wrox
 
Join Date: Jun 2003
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
================================================== =========
 
Old September 3rd, 2007, 02:19 PM
Friend of Wrox
 
Join Date: Mar 2006
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


 
Old September 3rd, 2007, 02:23 PM
Friend of Wrox
 
Join Date: Jun 2003
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
================================================== =========
 
Old September 3rd, 2007, 02:46 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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
 
Old September 3rd, 2007, 02:52 PM
Friend of Wrox
 
Join Date: Jun 2003
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
================================================== =========





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





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