• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

Find a specific object in a generic List collection

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
5281
Asked:
5281
  • 8
  • 7
1 Solution
 
Fernando SotoRetiredCommented:
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
 
5281Author Commented:
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
 
Fernando SotoRetiredCommented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
5281Author Commented:
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
 
Fernando SotoRetiredCommented:
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
 
5281Author Commented:
Thank you.
0
 
Fernando SotoRetiredCommented:
Not a problem, glad to help.
0
 
5281Author Commented:
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
 
Fernando SotoRetiredCommented:
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
 
5281Author Commented:
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
 
5281Author Commented:
I can use this way.  

For Each Item In customer

                        Response.Write(Item.ContactName & " ")
                        Response.Write(Item.City & " ")
Next
0
 
Fernando SotoRetiredCommented:
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
 
Fernando SotoRetiredCommented:
Yes, that is the way.
0
 
5281Author Commented:
Thanks a lot.
0
 
Fernando SotoRetiredCommented:
Not a problem, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now