c# - 通过特定属性将列表转换为逗号分隔字符串的最简单方法?

标签 c# .net linq entity-framework

我有一个自定义对象列表,实际上这些是我存储在 IEnumerable 集合中的实体。我想将列表转换为逗号分隔的字符串,但我只想要一个特定的属性,How do I build a comma separated string with a specific property from a custom object list?

我知道我可以使用 "Foreach/For (int i .... " 构建一个逗号分隔的列表 但我认为有一种简单且更好的方法 那么简单的方法是什么?

这是我的 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));

关于c# - 通过特定属性将列表转换为逗号分隔字符串的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20185084/

相关文章:

c# - 用于统计的 SQL 单独数据集 - DataTable 的集合

.net - VB.NET 中的“阴影”与 'Overrides'

c# - 扩展 LINQ 表达式

c# - Microsoft.Office.Interop.Excel : How to Apply a border to ONE CELL

c# - 从后面的代码更改母版页

.net - 为什么我的 .NET 应用程序会产生如此多的页面错误?

.net - 将 UTF8 字符串解码为拉丁文

c# - .net linq with regex ismatch in where

c# - 将列表转换为列表<string>

c# - 在 C# 'Dataset' 中获取标识列值 --Access 数据库