Tuesday, September 10, 2013

How to convert a List to a comma separated string in C#

Here i am showing how to convert a List<int> to a comma separated string,
where
 malist is a list of PublisherID (int),
manList.Distinct() is distict list of PublisherID i.e removed all duplicate items from the malist and
objModel.TempCommaPublisher is s string 

Here is the sample code

List<int> manList = (from item in model.SearchList orderby item.PublisherID ascending select item.PublisherID).ToList();
           
            if (string.IsNullOrEmpty(objModel.TempCommaPublisher))
                objModel.TempCommaPublisher = string.Join<int>(",", manList.Distinct());


Here string.Join<int>(",", manList.Distinct()); join all int items from manList.Distinct() separated by a "," to a string .

No comments:

Post a Comment