- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试如下所示进行字符串同情:
var nDetails = listOfServiceUrls.Where(x => String.Equals(x.Description,
serviceName,StringComparison.InvariantCultureIgnoreCase));
但是得到异常,
System.ArgumentException: Incorrect number of arguments supplied for call to method 'Boolean Equals(System.String, System.String, System.StringComparison)'
如果我删除 where 子句它就可以正常工作!!
var nDetails = listOfServiceUrls.Where(x => String.Equals(x.Description,serviceName));
最佳答案
您必须使用 Entity Framework 或类似技术,它们实际上需要解释作为参数提供给 Where
的表达式方法。它只会翻译一小部分基类库(标准 .NET 库)方法及其重载。
将光标悬停在您的 listOfServiceUrls
上代码编辑器中的变量并查看它是什么类型。我的钱是因为它是 IQueryable<string>
(与 IEnumerable<string>
相对)。
EF 似乎确实对实例 Equals(string, StringComparison)
进行了翻译System.String
上的方法, 因此您可以按如下方式重写代码:
var nDetails = listOfServiceUrls.Where(
x => x.Description.Equals(serviceName, StringComparison.InvariantCultureIgnoreCase));
与 .NET 不同,您不会看到 NullReferenceException
如果集合中的任何字符串恰好是 NULL
- 因为谓词将由 SQL Server 而不是 .NET 运行时执行。
关于c# - 为什么 StringComparison.InvariantCultureIgnoreCase 在 where 子句中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45272308/
做了很多不区分大小写的字符串比较,我以很多冗长的语句结束,比如: myList.FirstOrDefault( c => string.Equals( c.Inter
我正在将 C# 代码移植到 Windows 应用商店应用程序。令我惊讶的是,以下代码不再有效: someString.Equals("someOtherString", StringCompariso
我正在尝试填充包含用户提供的字母的所有结果。 foreach (var item in entries.PhonebookList.Where(ab => ab.FirstName.Contains(
在C#中做字符串比较时,和a有什么区别 string test = "testvalue"; test.Equals("TESTVALUE", StringComparison.CurrentCult
这个问题在这里已经有了答案: Which is generally best to use — StringComparison.OrdinalIgnoreCase or StringCompari
我正在尝试如下所示进行字符串同情: var nDetails = listOfServiceUrls.Where(x => String.Equals(x.Description, serviceN
我认为 StringComparison.OrdinalIgnoreCase 和 StringComparison.InvariantCultureIgnoreCase 在涉及纯英文字符串时做同样的工
在 this page ,一位评论者写道: 永远不要使用 .ToUpper 来确保比较字符串时不区分大小写。 取而代之的是: type.Name.ToUpper() == (controllerNam
我有一些这样的代码: If key.Equals("search", StringComparison.OrdinalIgnoreCase) Then DoSomething() End If
StringComparer.InvariantCultureIgnoreCase Equals 对“”与“\0”返回 true,但 GetHashCode 对两个字符串返回不同的值。这是错误吗? v
当我不区分大小写地比较两个字符串时,我使用: if (s1.Equals(s2, StringComparison.InvariantCultureIgnoreCase)) ... 我想在整个代码中缩
以下查询有效。当我用错误的大小写输入名称时,我得到了正确的结果。 private static IObjectContainer db = Db4oFactory.OpenFile(db4oPath)
我是一名优秀的程序员,十分优秀!