c# - 使用 LINQ 我如何连接集合中项目的字符串属性

标签 c# linq

我有一个集合中的对象列表。每个对象都有一个名为 Issue 的字符串属性。我想连接集合中所有项目的问题并将它们放入一个字符串中。使用 LINQ 执行此操作的最简洁方法是什么。

这里是手动方式:

 string issueList = "";
 foreach (var item in collection)
 {
       if (!String.IsNullOrEmpty(item.Issue)
       {
             issueList = issueList + item.Issue + ", ";
       }
 }
 //Remove the last comma
 issueList = issueList.Remove(issueList.Length - 2);
 return issueList;

最佳答案

你可以写

return String.Join(", ", collection.Select(o => o.Issue));

在 .Net 3.5 中,您需要添加 .ToArray()

关于c# - 使用 LINQ 我如何连接集合中项目的字符串属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5615641/

相关文章:

c# - 找不到预期的}

c# - Linq 和 ListView 以及包含

c# - Linq 查询计数和 GroupBy

c# - LINQ to Objects - ICollection 属性到 CSV 的一般转换

php - 带有 LinQ 过滤器和 jQuery tmpl 示例的 jQuery AJAX 请求

c# - 为什么输出是 6 而不是其他数字?

c# - 如何使用 System.Drawing 将旋转的字符串绘制为图像?

c# - 通过 C# 使用 PlotKit (javascript)

c# - PHP - 如何使用变量访问 mysql_fetch_assoc 的值?

entity-framework - 使用system.linq.dynamic连接表