为调用'布尔等号方法提供的参数数量不正确
本文关键字:参数 数数 不正确 方法 调用 布尔 | 更新日期: 2023-09-27 18:13:50
为什么我得到一个参数异常,说我传递错误数量的参数到字符串。=方法?
传递三个参数,这应该是正确的。实际上,它应该抛出一个编译时错误,而不是运行时…
看到错误了吗?
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)'
首先,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问题描述了各种方法
这是一个运行时错误,因为你可能是在运行Linq查询提供程序,它接受c#编译器从你的c#代码中创建的表达式,并在运行时执行它。提供者可能无法转换这个Equals重载。
尝试将Linq查询更改为:
(from l in context.Languages
join t in context.Translations on l.ISO639_ISO3166 equals t.ISO639_ISO3166).AsEnumerable()
.Where(l => string.Equals(l.ApplicationName, applicationName, StringComparison.InvariantCultureIgnoreCase))
.Select(new Translation
{
Key = t.Key,
Text = t.Text
}).ToListAsync();
在Linq中,您需要使用equals的静态方法。这样做就可以了:
where l.ApplicationName.Equals(applicationName, StringComparison.InvariantCultureIgnoreCase)
另一种方法(如果您坚持不区分大小写的比较)
var translations = await (from l in context.Languages
join t in context.Translations on l.ISO639_ISO3166 equals t.ISO639_ISO3166
where l.ApplicationName.Equals(applicationName,StringComparison.CurrentCultureIgnoreCase)
select new Translation
{
Key = t.Key,
Text = t.Text
}).ToListAsync();
注意:
l.ApplicationName.Equals(applicationName,StringComparison.CurrentCultureIgnoreCase)
但是很可能你不需要这个,因为Linq to SQL默认是
" . .不区分大小写,或者更确切地说,最常见的排序规则是不区分大小写…"
如果你可以选择这个,这是另一种不区分大小写的比较方式:
where l.ApplicationName.ToLower() == applicationName
,其中applicationName
可以作为applicationName.ToLower()
传递,因此这成为不区分大小写的比较:p