Converting a Generic List of Custom Objects & Property to a Formatted String List

Technorati Tags:

Today I had the need to take a list of names from a returned list of Custom Objects, and convert one of its properties it to a string that included some HTML.  This is nothing fancy, and pretty standard/simple but I thought I’d post about it anyway.  Since I did not need a custom ToString() method here to convert all properties of my object, I just created a simple method to iterate through the list of objects and add one of the properties to the StringBuilder object then return my string with HTML.

The reason I needed to do this, was because a customer selects for example a list of Entrees in a Web Form.  I need to send that list formatted with HTML in an email to a client:

entrees-checkboxes

So after the customer selects the values, submits them, then now I want to retrieve them and include the selected items in an HTML email.

First, since the retrieved list was just a list of Entrees from my Data Layer Custom Class, and my class does not implement IEnumerable (yet that is), I can’t just loop through this list of custom objects.  So I in the meantime, I shoved it into a generic list first just so that I can iterate through the list of objects or sort them if I wish:

   1: List<Entree> entrees;
   2: entrees = Entree.RetrieveAllByOrderID(request.OrderID);

And now I’m creating a string variable and assigning to it a formatted string result with some HTML, by calling a method which iterates through the generic list, and shoves the Entree Name (which is a property of the Entree class)  into some sort of HTML formatted list; in this case I used line breaks:

   1: string entreesList = ConvertEnteesListToHTMLString(entrees);

The method which is pretty common, nothing special.  It just accepts an incoming generic list of Entree objects, loops through each Entree and converts the property EntreeName to a string and concatenates it using a StringBuilder and appends am HTML line break between each:

   1: private static string ConvertEnteesListToHTMLString(List<Entree> entreesList)
   2: {
   3:     StringBuilder sb = new StringBuilder();
   4:     foreach (Entree entree in entreesList)
   5:     {
   6:         sb.Append(entree.EntreeName.ToString());
   7:         sb.Append("<br>");
   8:     }
   9:     string html = sb.ToString();
  10:     return html;
  11: }

Now the string I’m sending as part of the HTML email looks like this:

"Carne Asada<br>Carnitas<br>Carne Desebrada<br>Chicken Mole<br>"

And obviously then the owner sees a nice list of entrees selected by their customer in the email:

Carne Asada

Carnitas

Chicken Mole


Print | posted on Monday, October 13, 2008 12:15 AM

Comments on this post

# re: Converting a Generic List of Custom Objects & Property to a Formatted String List

Requesting Gravatar...
This looks like a great use for an extension method. You already have taken the pain to create the static, now take the next step and make it an extension.

You new code would look like this:
string entreesList = entrees.ToHTMLString();

Much cleaner and easier to read
Left by Derik Whittaker on Oct 13, 2008 5:24 AM

# re: Converting a Generic List of Custom Objects & Property to a Formatted String List

Requesting Gravatar...
@Derik

Definitely a good idea. Just have not gotten to that point yet while I was coding this out but yes, would make great sense.
Left by Dave Schinkel on Oct 14, 2008 12:05 AM

Your comment:

 (will show your gravatar)
 
Please add 2 and 3 and type the answer here: