Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to convert list to a comma separated string by a Specific Property?

I have a list of Custom objects ,Actually those are entities I am storing in an IEnumerable collection. I want to convert the list to a comma separated string, but I want only one specific property, How do I build a comma separated string with a specific property from a custom object list?

I know i can build a comma separated list by using a "Foreach / For (int i .... " but I think there is a easy and a better way for this So what would be that easy way?

This is my list

IEnumerable<BAL.Category> categories = chklCategories.CheckedItems.Cast<BAL.Category>();
            //Category object has a property called Name , I want the list from that property
like image 427
user3030035 Avatar asked Nov 25 '13 05:11

user3030035


People also ask

How do you convert a list to a comma separated string?

A List of string can be converted to a comma separated string using built in string. Join extension method. string. Join("," , list);

How do you convert a list to a comma separated string in python?

How to Convert a Python List into a Comma-Separated String? You can use the . join string method to convert a list into a string. So again, the syntax is [seperator].

How do you separate array values with commas?

Array join() method This method joins the elements of the array to form a string and returns the new string. The elements of the array will be separated by a specified separator. If not specified, Default separator comma (, ) is used.


1 Answers

This is very easy , Isn't it ?

string sCategories = string.Join(",", categories.Select(x => x.Name));
like image 98
Kas Avatar answered Oct 05 '22 23:10

Kas