Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

October 30, 2010

Building a List of of Items with Child Items

Filed under: C#,VB.NET @ 7:46 pm

In this prior post, I demonstrated how to build a generic list of items. The example built a list of individual customers. This post demonstrates how to build a generic list of items where one of those items is another generic list.

In this example, the customer is no longer a single person, but rather a company with a set of contact persons.

Here is a first draft of a Contact class and a Customer class that contains a list of contacts.

In C#:

public class Contact
{
    public int ContactId { get; set; }
    public int CustomerId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
}

public class Customer
{
    public int CustomerId { get; set; }
    public string CompanyName { get; set; }
    public List<Contact> ContactList { get; set; }
}

In VB:

Public Class Contact
    Public Property ContactId As Integer
    Public Property CustomerId As Integer
    Public Property FirstName As String
    Public Property LastName As String
    Public Property EmailAddress As String
End Class

Public Class Customer
    Public Property CustomerId As Integer
    Public Property CompanyName() As String
    Public Property ContactList As List(Of Contact)
End Class

Both of these classes use auto-properties, which is the one-line technique for defining property procedures. (See this post for more information on auto-properties.)

And here is a single line of code to populate the list of Customers.

In C#:

List<Customer> custList = new List<Customer> {
new Customer() {
   CustomerId = 1,
   CompanyName = "ME Industries",
   ContactList = new List<Contact>() {
      new Contact()  {
       ContactId = 1,
       CustomerId = 1,
       FirstName = "Bilbo",
       LastName = "Baggins",
       EmailAddress = "bb@hob.me"},
     new Contact()  {
       ContactId = 2,
       CustomerId = 1,
       FirstName = "Frodo",
       LastName = "Baggins",
       EmailAddress = "fb@hob.me"}}},
new Customer()  {
    CustomerId = 2,
    CompanyName = "Farmers Trust",
    ContactList = new List<Contact>()  {
      new Contact()  {
         ContactId = 3,
         CustomerId = 2,
         FirstName = "Samwise",
         LastName = "Gamgee",
         EmailAddress = "sg@hob.me"},
      new Contact()  {
         ContactId = 4,
         CustomerId = 2,
         FirstName = "Rosie",
         LastName = "Cotton",
         EmailAddress = "rc@hob.me"}}}};

In VB:

Dim custList As New List(Of Customer) From {
   New Customer() With {
      .CustomerId = 1,
      .CompanyName = "ME Industries",
      .ContactList = New List(Of Contact) From {
        New Contact() With {
          .ContactId = 1,
          .CustomerId = 1,
          .FirstName = "Bilbo",
          .LastName = "Baggins",
          .EmailAddress = "bb@hob.me"},
        New Contact() With {
          .ContactId = 2,
          .CustomerId = 1,
          .FirstName = "Frodo",
          .LastName = "Baggins",
          .EmailAddress = "fb@hob.me"}}},
    New Customer() With {
      .CustomerId = 2,
      .CompanyName = "Farmers Trust",
      .ContactList = New List(Of Contact) From {
        New Contact() With {
            .ContactId = 3,
            .CustomerId = 2,
            .FirstName = "Samwise",
            .LastName = "Gamgee",
            .EmailAddress = "sg@hob.me"},
        New Contact() With {
            .ContactId = 4,
            .CustomerId = 2,
            .FirstName = "Rosie",
            .LastName = "Cotton",
            .EmailAddress = "rc@hob.me"}}}}

Wow! That is one long line of code! Notice how close the VB and C# syntax is for this line.

This code creates two new customers defined with the two "new Customer()" statements.  For each customer it creates two contacts. This code uses a technique called collection initializers. (For more information on collection initializers, see this post.)

Any time you have a related set of "child" data, you can associate that data with the "parent" by defining a List<child>. Initializing it in one line of code is completely optional. 🙂

Enjoy!

3 Comments

  1.   Chan Beauvais — December 2, 2010 @ 10:33 am    Reply

    I am having a great deal of trouble trying to implement a parent child relationship.
    I need to add a child to the parent’s child list collection property, then save the parent. This would then hopefully save the child in the list collection to the database.
    Any code samples?

    P.S. Working in 2008 v 3.5…

  2.   how to build a list — July 7, 2011 @ 10:43 am    Reply

    i am a blogger everyday i browse different types of articles to get lots of new tips while i was browsing,i found ur blog your article is really awesome.Thanks for sharing such nice article here.i also want to share valuable how to build a list tips.

  3.   Ray Toresi — November 21, 2013 @ 8:55 am    Reply

    Can you please show or provide a link to another article that show a way to do the same but without the collection initializers so that the customer list can be populated from a database of file.

    Thanks

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2019 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs