by
1
7
2,623
5
Top 1% !
Popular
Famous
Tagged
Specified
Refined
OpenSource
Popularity: 7101st place
Created
Modified Feb 1, 2014

Published on:

.NETFramework4.0
Architecturex86_64
Extension.cs
InputName0idList
InputName1removeZerosOrNegatives
InputName2separator
InputType0ICollection<T>
InputType1bool
InputType2char
LanguageCSharp
LicenseMIT_X11
MethodNameMergeIntoCommaSeparatedString
OSWindows
OutputTypestring

C#: Prepare a string of comma-separated values from a collection.

This method prepares a comma-separated (or any other character specified as 'separator' input) from the given list. It can also remove zero or negative inputs if explicitly required by setting removeZerosOrNegatives=true
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/25656/C---Prepare-a-string-of-comma-separated-?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
public static string MergeIntoCommaSeparatedString<T>(ICollection<T> idList, bool removeZerosOrNegatives, char separator = ',') where T : struct { string list_ids = string.Empty; if (idList != null) { foreach (T id in idList) { if (removeZerosOrNegatives && (Convert.ToInt64(id) <= 0)) continue; list_ids += string.Format("{0}{1}", separator, id); } list_ids = list_ids.TrimStart(separator); } return list_ids; }
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus