7

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

2 Answers 2

16

This is very easy , Isn't it ?

string sCategories = string.Join(",", categories.Select(x => x.Name));
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.