Solved

Converting Generic Lists to Strings using C#

Posted on 2006-11-29
3
1,055 Views
Last Modified: 2007-12-19
Let's say I have two generic lists: one is a g-list of integers (List<int>) and the other is of a custom class (List<StoredProcedureInfo>).  First, I want to convert the list of integers to a comma-delimited string:

=================================
List<int> integerList = new List<int>();
integerList.Add(4);
integerList.Add(132);
integerList.Add(36);
// How do I retrieve the values in [integerList] as a comma-delimited string?
=================================


Let's say my custom class, StoredProcedureInfo, has four properties: ID (int), Name (string), Text (string), Checked (bool).  Is there an easy way to get all of the ID property values in List<StoredProcedureInfo> as a comma-delimited string?


-= DeathToSpam =-
0
Comment
Question by:deathtospam
[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
  • 2
3 Comments
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 500 total points
ID: 18040602
Hi, you can use the following method, it accepts a generic List and a string indicating the property to get the underlying data for CSVformat (see example below):

        private string GetCSVStringFromList<T>(List<T> list, string property)
        {
            string csv = string.Empty;

            if (property == null || property == string.Empty)
            {
                foreach (T item in list)
                    csv += item + ",";
            }
            else
            {
                try
                {
                    foreach (T item in list)
                        csv += item.GetType().GetProperty(property).GetValue(item, null) + ",";
                }
                catch
                {
                    return null;
                }
            }
           
            return (csv == string.Empty) ? null : csv.TrimEnd(',');
        }

To get a CSV formatted string of your integer list using above method:

      string integerListCSV = GetCSVStringFromList(integerList, null);

To get a CSV formatted string of your ID properties in a List<StoredProcedureInfo>:

      string idListCSV = GetCSVStringFromList(myStoredProcedureList, "ID");

      if (idListCSV != null)
      {
               //csv string was constructed ok...
      }

The method returns null if an error occurrs when using reflection to get the property values and also when the input list is empty.
0
 
LVL 9

Author Comment

by:deathtospam
ID: 18046540
That's what I'm looking for.  But there's no built-in method of a generic list that offers the same functionality, is there?


-= DeathToSpam =-
0
 
LVL 18

Expert Comment

by:Ravi Singh
ID: 18046598
Hi, not that I'm aware of... the String.Join allows you to create a CSV string by specifying a delimiter and a string array whose values will be concatenated into one string (seperated by the delim you specify as the first param)... but there doesnt seem to be a prewritten method that you can use for this type of functionality with generic lists... let alone generic lists with a specific property of the underlying type (like your ID property).


Hope this helps!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio hangs on running project 6 84
VB.NET - Refactor Class per SOLID principles 2 43
C# Windows app updating JSON file error. 9 50
array not updating 8 42
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

739 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