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?
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):
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
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
private string GetCSVStringFromList<T>(Li
{
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
}
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(integ
To get a CSV formatted string of your ID properties in a List<StoredProcedureInfo>:
string idListCSV = GetCSVStringFromList(mySto
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.