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

.net c# entity-framework linq

Question

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
1
6
11/25/2013 5:16:27 AM

Accepted Answer

This is very easy , Isn't it ?

string sCategories = string.Join(",", categories.Select(x => x.Name));
13
11/25/2013 5:03:42 AM

Popular Answer

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));
}


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow