惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

月光博客
月光博客
雷峰网
雷峰网
S
SegmentFault 最新的问题
博客园 - 【当耐特】
博客园_首页
量子位
爱范儿
爱范儿
博客园 - 叶小钗
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Jina AI
Jina AI
V
V2EX
美团技术团队
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
宝玉的分享
宝玉的分享
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

Donghai's Blog

使用DBeaver连接Dynamics 365 Dataverse | Donghai C# 技术备忘 - LINQ | Donghai 为Astro站点添加Google Analytics | Donghai C# 技术备忘 - LINQ - Donghai’s Blog 20260819 说些什么 | Donghai Dynamics 365 interview questions | Donghai Dynamics 365 事件框架、事件执行管道 | Donghai 马拉松配速表 | Donghai 马拉松赛事分级 | Donghai 抖音创作日志(2026年) | Donghai 2026年跑步日志(5月开始) | Donghai 2FA | Donghai 初尝Github Actions | Donghai AstroPaper主题添加Waline评论 | Donghai Dynamics 365集中视图/聚焦视图/Focused View | Donghai AstroPaper主题添加GitHub风格的Markdown警告框 | Donghai AstroPaper主题自定义记录 | Donghai 我日常收藏的优质网站资源导航(持续更新) | Donghai 如何从归档页批量获取URL并提交到Bing Webmaster Tools | Donghai PaperMod 使用 Zeoseven 自定义网页字体 | Donghai 我常用的Prompt | Donghai Hugo默认时区导致本地预览文章不显示 | Donghai 通过手动提交工单解决Bing不收录网站问题 | Donghai 如何访问 Power BI 管理门户(Power BI Admin Portal) | Donghai 还在手敲时间戳?Win11输入法一个技巧,秒输当前时间戳 | Donghai 工作术语备忘录(持续更新) | Donghai 使用XrmToolBox导出安全角色表级权限到Excel | Donghai 24年的年终总结 | Donghai World笔记 | Donghai Excel笔记 | Donghai
C# 技术备忘 (1) | Donghai
Donghai · 2026-08-19 · via Donghai's Blog

Table of contents

Open Table of contents
  • 241115-错误消息列表的格式化输出
    • 两种方式对比

241115-错误消息列表的格式化输出

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApp
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // 模拟错误消息列表
            List<string> errorMsgList = new List<string>
            {
                "Field 'xxx1' is empty.",
                "Field 'xxx2' is empty.",
                "Field 'xxx3' is empty.",
                "Field 'xxx4' is empty."
            };

            if (errorMsgList != null && errorMsgList.Any())
            {
                // ===== 方式1:使用 StringBuilder =====
                Console.WriteLine("=========[方式1]=========");
                StringBuilder valiMesStrBui = new StringBuilder();
                int index = 1;
                foreach (string item in errorMsgList.Distinct())
                {
                    valiMesStrBui.AppendLine($"{index}.{item}");
                    index++;
                }
                valiMesStrBui.AppendLine("存在上述异常,请维护后再尝试提交");
                Console.WriteLine(valiMesStrBui.ToString());

                // ===== 方式2:使用 string.Join + LINQ Select =====
                Console.WriteLine("=========[方式2]=========");
                string errorMessages = string.Join(
                    Environment.NewLine,
                    errorMsgList.Select((string msg, int i) =>
                    $"{i + 1}.{msg}")) +
                    "\n 存在上述异常,请维护后再尝试提交";
                Console.WriteLine(errorMessages);
            }

            Console.ReadKey();
        }
    }
}

两种方式对比

特性方式1:StringBuilder方式2:string.Join + LINQ
实现方式命令式循环(foreach),逐步追加字符串函数式风格(Select),一次性连接
可读性步骤清晰,容易理解,适合初学者代码更简洁、紧凑,表达力强,符合函数式编程习惯
性能在循环内多次追加,但StringBuilder保证了高效的内存使用Select中生成中间字符串集合,然后一次性连接。对于小列表,差异可忽略
索引处理手动维护 index 变量利用Select的重载,直接获取索引 i
扩展性易于在循环中添加复杂逻辑(如条件过滤)易于链式调用其他LINQ方法(如WhereOrderBy
换行符使用使用AppendLine,自动添加平台相关的换行符使用Environment.NewLine显式指定,更明确

(完)

如果这篇文章刚好帮到了你,欢迎请我喝杯咖啡