C# 定义字典

方法一

C# 复制代码
       Dictionary<string, string> StateCodesDic = new Dictionary<string, string>();
            StateCodesDic.Add("Johor", "01");
            StateCodesDic.Add("Kedah", "02");
            StateCodesDic.Add("Kelantan", "03");
            StateCodesDic.Add("Melaka", "04");

            StateCodesDic.Add("Negeri Sembilan", "05");
            StateCodesDic.Add("Pahang", "06");
            StateCodesDic.Add("Pulau Pinang", "07");
            StateCodesDic.Add("Perak", "08");
            StateCodesDic.Add("Perlis", "09");

            StateCodesDic.Add("Selangor", "10");
            StateCodesDic.Add("Terengganu", "11");
            StateCodesDic.Add("Sabah", "12");
            StateCodesDic.Add("Sarawak", "13");
            StateCodesDic.Add("Wilayah Persekutuan Kuala Lumpur", "14");
            StateCodesDic.Add("Wilayah Persekutuan Labuan", "15");
            StateCodesDic.Add("Wilayah Persekutuan Putrajaya", "16");
            StateCodesDic.Add("Not Applicable", "17");

方法二

你的代码逻辑是要根据 tempStateID = "KUALA" 匹配到 "Wilayah Persekutuan Kuala Lumpur" 并返回对应的编码 "14"

问题分析

  1. 当前代码的问题

    • 你使用了 lowerCaseDictionary,但 "KUALA" 并不直接匹配 "wilayah persekutuan kuala lumpur"(全小写)。
    • 你的 FindModel 使用了 Contains 查询,但 "kuala" 只是部分匹配,需要确保查询方式正确。
  2. 优化方案

    • 直接遍历 StateCodesDic,检查 Key 是否包含 tempStateID(不区分大小写)。
    • 使用 StringComparison.OrdinalIgnoreCase 进行不区分大小写的比较。

修改后的代码

csharp 复制代码
public ActionResult TestDemo()
{
    Dictionary<string, string> StateCodesDic = new Dictionary<string, string>
    {
        { "Johor", "01" },
        { "Kedah", "02" },
        { "Kelantan", "03" },
        { "Melaka", "04" },
        { "Negeri Sembilan", "05" },
        { "Pahang", "06" },
        { "Pulau Pinang", "07" },
        { "Perak", "08" },
        { "Perlis", "09" },
        { "Selangor", "10" },
        { "Terengganu", "11" },
        { "Sabah", "12" },
        { "Sarawak", "13" },
        { "Wilayah Persekutuan Kuala Lumpur", "14" },
        { "Wilayah Persekutuan Labuan", "15" },
        { "Wilayah Persekutuan Putrajaya", "16" },
        { "Not Applicable", "17" }
    };

    string tempStateID = "KUALA";
    string msg = "17"; // 默认值,未匹配时返回 "17"
    dynamic FindModel = new { Key = "", Value = "" };

    // 遍历字典,查找 Key 包含 "KUALA"(不区分大小写)的项
    foreach (var item in StateCodesDic)
    {
        if (item.Key.IndexOf(tempStateID, StringComparison.OrdinalIgnoreCase) >= 0)
        {
            FindModel = new { Key = item.Key, Value = item.Value };
            msg = item.Value;
            break; // 找到第一个匹配项后退出循环
        }
    }

    return Json(new
    {
        Data = FindModel,
        keyCode = msg
    });
}

关键优化点

  1. 直接遍历 StateCodesDic
    • 不需要额外转换大小写,直接使用 IndexOf + StringComparison.OrdinalIgnoreCase 进行模糊匹配。
  2. 更高效的匹配方式
    • item.Key.IndexOf(tempStateID, StringComparison.OrdinalIgnoreCase) >= 0 检查 Key 是否包含 "KUALA"(不区分大小写)。
  3. 默认值处理
    • 如果没有匹配项,默认返回 "17"(对应 "Not Applicable")。

测试结果

  • tempStateID = "KUALA" 时,会匹配到 "Wilayah Persekutuan Kuala Lumpur",返回:

    json 复制代码
    {
        "Data": { "Key": "Wilayah Persekutuan Kuala Lumpur", "Value": "14" },
        "keyCode": "14"
    }
  • 如果 tempStateID = "JOHOR",则返回:

    json 复制代码
    {
        "Data": { "Key": "Johor", "Value": "01" },
        "keyCode": "01"
    }
  • 如果 tempStateID = "XXX"(无匹配),则返回:

    json 复制代码
    {
        "Data": { "Key": "", "Value": "" },
        "keyCode": "17"
    }

这样就能正确获取到 "14" 这个编码了! 🚀

相关推荐
牛奔9 小时前
Go 如何避免频繁抢占?
开发语言·后端·golang
想用offer打牌14 小时前
MCP (Model Context Protocol) 技术理解 - 第二篇
后端·aigc·mcp
KYGALYX16 小时前
服务异步通信
开发语言·后端·微服务·ruby
掘了16 小时前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
爬山算法16 小时前
Hibernate(90)如何在故障注入测试中使用Hibernate?
java·后端·hibernate
Moment17 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
Cobyte17 小时前
AI全栈实战:使用 Python+LangChain+Vue3 构建一个 LLM 聊天应用
前端·后端·aigc
程序员侠客行18 小时前
Mybatis连接池实现及池化模式
java·后端·架构·mybatis
Honmaple18 小时前
QMD (Quarto Markdown) 搭建与使用指南
后端
PP东19 小时前
Flowable学习(二)——Flowable概念学习
java·后端·学习·flowable