c# - 一行 LINQ 将 string[] 展平为字符串?

标签 c# .net linq

我想出了下面的 foreach,但我希望这可以在一行中完成。也许是 linq?任何想法将不胜感激。

foreach (string item in decoder.AllKeys)
{
    message += String.Format("{0}: {1} ;", item, decoder[item]);
}

最佳答案

var message = string.Join(
    ";", 
    decoder.AllKeys
           .Select(x => string.Format("{0}: {1} ", x, decoder[item]))
           .ToArray()
);

关于c# - 一行 LINQ 将 string[] 展平为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6089215/

相关文章:

c# - FileStream 在应用程序冷启动时非常慢

c# - 如何让 ToString() 出现在 Debug 中

c# - LINQ group by 查询使用反射(reflect)的属性名称

c# - 当 ExecuteScalar 工作正常时,为什么 ExecuteReader 会崩溃读取某些记录的特定数据字段?

c# - 使用 ElasticLowLevelClient 客户端在 Elasticssearch 中批量索引

c# - 用*屏蔽掉字符串的前12个字符?

c# - LINQ 如何根据给定条件对项目进行排序?

c# - 在上传到 S3 的对象上设置 `Content-Disposition`?

c# - 即使已设置,变量也会导致 NullReferenceException

c# - LINQ-to-SQL 连接中的 DefaultIfEmpty 导致重复