Solved

Converting Generic Lists to Strings using C#

Posted on 2006-11-29
3
1,054 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 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