Meg*_*ark 1 c# linq arrays expression
我的项目工作正常,直到我不得不考虑一个字符串数组而不仅仅是一个......我不知道如何解决这个问题.这Country是此方法所在的当前类的属性.它曾经是一个单独的字符串,但现在是一个数组.
最初它看起来像这样:
private Expression<Func<Payment, bool>> CountryMatches()
{
if (Country.Length < 1) return Skip;
return payment => payment.Country.ToLower().Contains(Country.ToLower());
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚的是如何设置它,以便如果Country匹配中的任何字符串payment.Country...当然这是传回一个表达式...这是我最好的猜测(但显然不正确)如何做我需要做的事:
private Expression<Func<Payment, bool>> CountryMatches()
{
if (Country.Length < 1) return Skip;
return payment => payment.Country.ToLower() == Country.Any().ToLower();
}
Run Code Online (Sandbox Code Playgroud)
你想检查Country反对的所有内容payment.Country,如下所示:
return payment => Country.Any(
c => payment.Country.ToLower().Contains(c.ToLower()));
Run Code Online (Sandbox Code Playgroud)
也就是说,这是检查一个字符串是否是另一个字符串的一个相当糟糕的方法,主要是因为它通过一次又一次地转换为小写来完成许多不必要的工作.这是一个更好的方法:
return payment => Country.Any(
c => payment.Country.IndexOf(c, StringComparison.OrdinalIgnoreCase) >= 0);
Run Code Online (Sandbox Code Playgroud)