为什么我得到一个参数异常,说我传递了错误数量的参数给string.equals方法?
我传递了三个参数,这应该是正确的。实际上它应该抛出一个编译时错误而不是运行时错误。
你看到错误了吗?
var translations = await (from l in context.Languages
join t in context.Translations on l.ISO639_ISO3166 equals t.ISO639_ISO3166
where string.Equals(l.ApplicationName, applicationName, StringComparison.InvariantCultureIgnoreCase)
select new Translation
{
Key = t.Key,
Text = t.Text
}).ToListAsync();
更新
Test Name: GetTranslations
Test FullName: TaaS.IntegrationTests.Tests.TranslationRepositoryTests.GetTranslations
Test Source: C:\test\TaaS-WebApplication\TaaS.IntegrationTests\Tests\TranslationRepositoryTests.cs : line 17
Test Outcome: Failed
Test Duration: 0:00:00,0473367
Result StackTrace:
at System.Linq.Expressions.Expression.GetMethodBasedBinaryOperator(ExpressionType binaryType, Expression left, Expression right, MethodInfo method, Boolean liftToNull)
at System.Linq.Expressions.Expression.Equal(Expression left, Expression right, Boolean liftToNull, MethodInfo method)
at System.Data.Entity.Core.Objects.ELinq.LinqExpressionNormalizer.VisitMethodCall(MethodCallExpression m)
at System.Linq.Expressions.EntityExpressionVisitor.Visit(Expression exp)
at System.Linq.Expressions.EntityExpressionVisitor.VisitLambda(LambdaExpression lambda)
at System.Linq.Expressions.EntityExpressionVisitor.Visit(Expression exp)
at System.Linq.Expressions.EntityExpressionVisitor.VisitUnary(UnaryExpression u)
at System.Linq.Expressions.EntityExpressionVisitor.Visit(Expression exp)
at System.Linq.Expressions.EntityExpressionVisitor.VisitExpressionList(ReadOnlyCollection`1 original)
at System.Linq.Expressions.EntityExpressionVisitor.VisitMethodCall(MethodCallExpression m)
at System.Data.Entity.Core.Objects.ELinq.LinqExpressionNormalizer.VisitMethodCall(MethodCallExpression m)
at System.Linq.Expressions.EntityExpressionVisitor.Visit(Expression exp)
at System.Linq.Expressions.EntityExpressionVisitor.VisitExpressionList(ReadOnlyCollection`1 original)
at System.Linq.Expressions.EntityExpressionVisitor.VisitMethodCall(MethodCallExpression m)
at System.Data.Entity.Core.Objects.ELinq.LinqExpressionNormalizer.VisitMethodCall(MethodCallExpression m)
at System.Linq.Expressions.EntityExpressionVisitor.Visit(Expression exp)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter..ctor(Funcletizer funcletizer, Expression expression)
at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.CreateExpressionConverter()
at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassc.<GetResultsAsync>b__a()
at System.Data.Entity.Core.Objects.ObjectContext.<ExecuteInTransactionAsync>d__3d`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<ExecuteAsyncImplementation>d__9`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
at System.Data.Entity.Core.Objects.ObjectQuery`1.<GetResultsAsync>d__e.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Utilities.TaskExtensions.CultureAwaiter`1.GetResult()
at System.Data.Entity.Internal.LazyAsyncEnumerator`1.<FirstMoveNextAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Data.Entity.Infrastructure.IDbAsyncEnumerableExtensions.<ForEachAsync>d__5`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at TaaS.Repository.TranslationRepository.<GetTranslationsAsync>d__2.MoveNext() in C:\_REPOSITORIES\taas-application\TaaS-WebApplication\TaaS.Repository\TranslationRepository.cs:line 20
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at TaaS.IntegrationTests.Tests.TranslationRepositoryTests.GetTranslations() in C:\_REPOSITORIES\taas-application\TaaS-WebApplication\TaaS.IntegrationTests\Tests\TranslationRepositoryTests.cs:line 45
Result Message:
Test method TaaS.IntegrationTests.Tests.TranslationRepositoryTests.GetTranslations threw exception:
System.ArgumentException: Incorrect number of arguments supplied for call to method 'Boolean Equals(System.String, System.String, System.StringComparison)'
5条答案
按热度按时间kb5ga3dv1#
首先,SQL字符串比较是不区分大小写的,或者说,最常见的排序规则是不区分大小写的。
您根本不需要使用
String.Equals
。尝试执行查询 * 而不使用 *String.Equals
调用。如果由于某种原因,查询未能返回结果,则可能是参数或数据有问题。您应该尝试直接执行等效的SQL语句,并检查是否有任何匹配的结果。
只有当基础列的排序规则修改为字符串敏感的排序规则时,字符串大小写才是问题。这将是非常不寻常的。事实上,困难的部分是让LINQ to EF执行区分大小写的查询。
至于错误本身,它是由于
String.Equals
无法转换为SQL而引起的。LINQ本身 * 不 * 执行查询,它只是一种语言。LINQ提供程序负责将查询转换为基础语言并执行它们。一些提供程序,如LINQ to SQL,将解析它们所能解析的任何内容,将结果加载到内存中,并将其传递给LINQ to Object以执行不支持的操作。这通常会导致 * 非常 * 糟糕的性能。在您的例子中,您的查询将加载内存中的所有翻译,然后尝试过滤它们。
另一方面,LINQ to EF不允许这样做来防止性能问题。无法转换的查询不会被执行。特别是
String.Equals
不能转换为SQL,因为字符串比较是由特定于区域性的排序规则控制的。不存在与不变文化等同的内容。如果你的表 * 是 * 区分大小写的,你必须改变用于比较的排序规则,例如
Latin1_CI_AS
。此SO question描述了执行此操作的各种方法qybjjes12#
这是一个运行时错误,因为您可能正在运行Linq query provider,它会获取C#编译器从C#代码创建的expression并在运行时执行它。提供程序可能无法转换此Equals重载。
尝试将您的Linq查询更改为:
zf2sa74q3#
在Linq中,你需要使用equals的静态方法。这样做,它会工作:
au9on6nz4#
另一种方式(如果你坚持不区分大小写的比较)
注意事项:
但很可能您不需要这样做,因为LinqtoSQL是默认设置
..不区分大小写,或者更确切地说,最常见的排序规则是不区分大小写的...
cetgtptt5#
如果你可以选择这个,还有一种不区分大小写的比较方式:
其中
applicationName
可以作为applicationName.ToLower()
传递,因此这成为不区分大小写的比较:P