C#-Linq:在linq中使用string.equals进行比较时忽略大小写

问题描述 投票:0回答:1

如何在下面的代码中使用“ StringComparison”属性?

string _strVariable = "New York";

var _countVar = nestList
  .SelectMany(list => list)
  .Count(city => string.Equals(city, _strVariable));

下面尝试过,但是它们不起作用,会引发错误。

var _countVar = nestList
  .SelectMany(list => list)
  .Count(city => string.Equals(city, _strVariable,StringComparison.OrdinalIgnoreCase));

var _countVar = nestList
  .SelectMany(list => list)
  .Count(city => string.Equals(city, _strVariable,StringComparer.OrdinalIgnoreCase)); 
c# linq string-comparison
1个回答
0
投票

为什么不能使用ToLower()这样的字符串库函数>

var _countVar = nestList
  .SelectMany(list => list)
  .Count(city => city.ToLower() == _strVariable.ToLower);
© www.soinside.com 2019 - 2024. All rights reserved.