[英]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. 我有一个自定义对象列表,实际上这些是我存储在IEnumerable集合中的实体。 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? 我知道我可以通过使用"Foreach / For (int i .... "
来构建逗号分隔列表,但我认为有一个简单而且更好的方法。那么这将是多么简单的方法?
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
这很容易,不是吗?
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. 通过使用此版本的string.Join<string>
方法,您可以在加入之前减少集合的副本。
static string CombineList(IEnumerable categories)
{
return string.Join<string>(",", categories.Select(x => x.Name));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.