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
A List of string can be converted to a comma separated string using built in string. Join extension method. string. Join("," , list);
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].
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.
This is very easy , Isn't it ?
string sCategories = string.Join(",", categories.Select(x => x.Name));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With