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

C#属性模式匹配如何忽略大小写?多属性场景优化方案问询

C# 属性模式匹配忽略大小写的优化实现

单属性匹配的优化方案

你之前的单属性大小写忽略方案可以简化,无需提前转换属性值,直接在when子句中结合String.Equals指定忽略大小写的比较规则即可,代码更简洁且逻辑清晰:

private string InventorySortKeySelector(Inventory inventory)
{
    return inventory switch
    {
        { LocationCode: var loc } 
            when string.Equals(loc, "Location1", StringComparison.OrdinalIgnoreCase) 
            && inventory.SubLocationCode.In("67", "96") 
            => $"{inventory.LocationCode}_{inventory.SubLocationCode}_{inventory.Number}",
        { LocationCode: var loc } 
            when string.Equals(loc, "Location2", StringComparison.OrdinalIgnoreCase)
            => $"Location2_ElseWhere_{inventory.Number}",
        { LocationCode: var loc } 
            when string.Equals(loc, "ElseWhere", StringComparison.OrdinalIgnoreCase)
            => $"Location2_ElseWhere_{inventory.Number}x",
        _ => $"{inventory?.LocationCode}_{inventory?.Number}"
    };
}

多属性同时匹配忽略大小写的实现

针对你提到的多属性(如LocationCode+ZipCode)匹配场景,同样可以通过when子句同时判断多个属性的大小写忽略匹配,完美覆盖需求:

private string InventorySortKeySelector(Inventory inventory)
{
    return inventory switch
    {
        { LocationCode: var loc, ZipCode: var zip } 
            when string.Equals(loc, "Location1", StringComparison.OrdinalIgnoreCase) 
            && string.Equals(zip, "1111", StringComparison.OrdinalIgnoreCase)
            && inventory.SubLocationCode.In("67", "96") 
            => $"{inventory.LocationCode}_{inventory.SubLocationCode}_{inventory.Number}",
        { LocationCode: var loc, ZipCode: var zip } 
            when string.Equals(loc, "Location2", StringComparison.OrdinalIgnoreCase) 
            && string.Equals(zip, "2222", StringComparison.OrdinalIgnoreCase)
            => $"Location2_ElseWhere_{inventory.Number}",
        { LocationCode: var loc, ZipCode: var zip } 
            when string.Equals(loc, "ElseWhere", StringComparison.OrdinalIgnoreCase) 
            && string.Equals(zip, "3333", StringComparison.OrdinalIgnoreCase)
            => $"Location2_ElseWhere_{inventory.Number}x",
        _ => $"{inventory?.LocationCode}_{inventory?.Number}"
    };
}

进一步简化代码的小技巧

如果需要多次使用大小写忽略比较,可以封装一个辅助方法减少重复代码:

// 辅助方法:忽略大小写比较字符串
private static bool EqualsIgnoreCase(string? input, string target)
{
    return string.Equals(input, target, StringComparison.OrdinalIgnoreCase);
}

// 使用辅助方法后的模式匹配
private string InventorySortKeySelector(Inventory inventory)
{
    return inventory switch
    {
        { LocationCode: var loc, ZipCode: var zip } 
            when EqualsIgnoreCase(loc, "Location1") 
            && EqualsIgnoreCase(zip, "1111")
            && inventory.SubLocationCode.In("67", "96") 
            => $"{inventory.LocationCode}_{inventory.SubLocationCode}_{inventory.Number}",
        // 其他分支同理...
        _ => $"{inventory?.LocationCode}_{inventory?.Number}"
    };
}

性能说明

使用StringComparison.OrdinalIgnoreCase进行比较是高效的选择,它不依赖文化区域信息,比CurrentCultureIgnoreCase的性能更优,同时避免了不同文化下的匹配差异问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 11:40:29