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
This is very easy , Isn't it ?
string sCategories = string.Join(",", categories.Select(x => x.Name));
Just try with this.
By using this version of the string.Join<string>
method, you can reduce copies of your collection before joining.
static string CombineList(IEnumerable categories)
{
return string.Join<string>(",", categories.Select(x => x.Name));
}