2017-07-18 36 views
1

使用C#我如何确保下面代码中的'val'对象是小写?将对象键转换为小写

case MultiValueUpdateMode.AddIfNotExist: 
if (value != null) 
{ 
    // Add the values only if they do not exist 
    foreach (var val in newValues) 
    { 
     if (!userDe.Properties["proxyAddresses"].Contains(val)) 
     userDe.Properties["proxyAddresses"].Add(val); 
    } 
} 
break; 
+0

你没有给我们足够的信息,但是你可以使用ToLower将()扩展方法的字符串。 –

回答

1

您可以通过使用ToString()转换任何对象的字符串,然后使用ToLower()让它是小写字母串:

string lowerCasedString = val?.ToString()?.ToLower(); //put null coaelescing just in case it is null 

然后使用它是这样的:

if (!userDe.Properties["proxyAddresses"].Contains(lowerCasedString)) 

附加说明:我通常也会删除前导和后续whi TE-空间利用Trim()

string cleanLowerCasedString = val?.ToString()?.ToLower()?.Trim(); //put null coaelescing just in case it is null 
+0

谢谢你,工作。 – user1226884

0
case MultiValueUpdateMode.AddIfNotExist: 
if (value != null) 
    { 
    // Add the values only if they do not exist 
    foreach (var val in newValues) 
    { 
     var lowerCaseVal = val.ToLower(); 
     if (!userDe.Properties["proxyAddresses"].Contains(lowerCaseVal)) 
     userDe.Properties["proxyAddresses"].Add(lowerCaseVal); 
    } 
    } 
break; 
+0

当我添加val = val.ToLower();你在哪里建议我得到intellisense错误'不能分配给'val',因为它是'foreach迭代变量'。我错过了别的吗? – user1226884

+0

@ user1226884更新。谢谢 – Ramankingdom