You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#代码优化需求:仅记录每个客户首个报错产品的行号

C#代码修改:仅记录每个客户首个报错产品

需求说明

当前代码会将每个客户的所有报错产品都写入HTML邮件文件,导致文件体积过大。要求调整为仅为每个客户的首个报错产品记录对应的Row_Number,后续同客户的报错不再生成记录,以此缩减文件体积。

修改思路

  1. 新增哈希集合跟踪已处理的报错客户,避免重复记录
  2. 仅当客户首次出现报错时,才生成带序号的报错行
  3. 修正原代码中的语法错误

修改后代码

public static void UpdateCompanies(List<Company> companies)
{
    if (companies.Count == 0)
        return;

    var tableRows = new StringBuilder();
    // 跟踪已记录过报错的客户编号
    var processedCustomers = new HashSet<string>();
    int counter = 0;

    foreach (Company company in companies)
    {
        ActionResponse actionResponse = UpdateCompany(company);

        if (!actionResponse.WasSuccessful)
        {
            // 仅处理客户的首次报错
            if (!processedCustomers.Contains(company.CustomerNo))
            {
                processedCustomers.Add(company.CustomerNo);
                counter++;
                
                string tableRow = File.ReadAllText(Path.Combine(_baseFilePath, "UpdateErrorsTableRow.html"));
                
                tableRow = tableRow.Replace("{{ ROW_NUMBER }}", counter.ToString());
                tableRow = tableRow.Replace("{{ CUSTOMER_NO }}", company.CustomerNo);
                tableRow = tableRow.Replace("{{ CUSTOMER_NAME }}", company.CustomerName);
                tableRow = tableRow.Replace("{{ ERROR_DATE }}", DateTime.Now.ToString());
                tableRow = tableRow.Replace("{{ ERROR_MESSAGE }}", actionResponse.ErrorMessage);

                tableRows.Append(tableRow);
            }
        }
    }

    string updateErrorsEmail = File.ReadAllText(Path.Combine(_baseFilePath, "UpdateErrors.html"));
    // 此处需补充将tableRows替换到邮件模板并发送邮件的逻辑(原代码未完成)
}

代码说明

  • processedCustomers哈希集合通过客户编号CustomerNo去重,确保每个客户仅被记录一次报错
  • 只有首次报错的客户才会触发counter自增和HTML行生成,后续同客户报错直接跳过,有效缩减文件体积
  • 修正了原方法参数的语法错误(UpdateCompanies<List<Company>> companies改为符合C#规范的UpdateCompanies(List<Company> companies))

内容的提问来源于stack exchange,提问作者Randy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 19:35:22