Solved

Find a specific object in a generic List collection

Posted on 2014-02-21
15
399 Views
Last Modified: 2014-02-25
I want to read the database into a generic List, and then compare the specific object in this List with other value.  I found a web site to show how to read database into generic List and bind gridview into generic list.

http://www.aspsnippets.com/Articles/How-to-bind-GridView-with-Generic-List-in-ASPNet-using-C-and-VBNet.aspx

But, I want to know how to find an object where CustomerId="10".  I used ASP.NET in VB, not C#, I need sample code.  Thank you.
0
Comment
Question by:5281
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
15 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39877742
Hi 5281;

The following code snippet using the info from the link you posted should do what you want.

Dim findCust As String = "10"
Dim customer As Customer = (From c in customers _
                            Where c.CustomerId = findCust _
                            Select c).SingleOrDefault()

Open in new window

0
 

Author Comment

by:5281
ID: 39877861
Thanks.  Where should I put that code snippet in the application?  Could you merge part of original code with your code together?  So I know where to add it?  Thank you.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39877882
Seeming that the code you requested finds a single record within a list of records. It can be placed in the code where you have access to the, List<Customers> customers, and know which CustomerId you are looking for and that those variables have been populated.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:5281
ID: 39877988
What does this code snippet return?   In your snippet, Dim customer as Customer, is customer a class?  How can I use customer (the result of query)?  Could you provide some code sample?  

The gridview is binded with generic list now.  How can I access the full record? Could you provide some code sample?

Thank you.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39878031
To your question, "What does this code snippet return?", it returns a single instance of a Customer object if any exist in the list that match the criteria stored in the variable findCust or a null value if none were found.

To your question, "In your snippet, Dim customer as Customer, is customer a class?", customer is an instantiated object which is of type Customer class.

To your question, "How can I use customer (the result of query)?",
Console.WriteLine("Customer ID is = " & customer.CustomerId)
Console.WriteLine("Contact Name is = " & customer. ContactName)
Console.WriteLine("City of customer is = " & customer.City)
Console.WriteLine("Country is = " & customer.Country)

Open in new window

0
 

Author Closing Comment

by:5281
ID: 39878092
Thank you.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39878140
Not a problem, glad to help.
0
 

Author Comment

by:5281
ID: 39886432
Fernando Soto,

If query produces multiple records (multiple customers), how to loop throug each record?  sample code please.  I will create a new question if you like.  Thanks.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39886566
Hi 5281;

Sorry I assume that you were querying the Customer table with a CustomerId, primary key, and thus should be the only one, apparently that's not the case, the following code snippet should return a List of Customers from the query.

Dim findCust As String = "10"
Dim customer As List(Of Customer) = (From c in customers _
                                     Where c.CustomerId = findCust _
                                     Select c).ToList()

Open in new window

0
 

Author Comment

by:5281
ID: 39886914
Thanks.  But how to get the specific object for that list (customer)?  How to write that loop to get the specifc item, like Contact Name, city?
0
 

Author Comment

by:5281
ID: 39886946
I can use this way.  

For Each Item In customer

                        Response.Write(Item.ContactName & " ")
                        Response.Write(Item.City & " ")
Next
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39886967
Hi 5281;

Just iterate through the collection of customer As shown in the code snippet below.

                                     
For Each row in customer
    Console.WriteLine("Contact Name : " & row.ContactName)
    ' ...
    Console.WriteLine("City : " & row.City
Next

Open in new window

0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39886973
Yes, that is the way.
0
 

Author Comment

by:5281
ID: 39886995
Thanks a lot.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39887004
Not a problem, glad to help.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question