gpt4 book ai didi

c# - 为调用方法“Boolean Equals”提供的参数数量不正确

转载 作者:IT王子 更新时间:2023-10-29 04:30:06 25 4
gpt4 key购买 nike

为什么我会收到参数异常,提示我将错误数量的参数传递给 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)'

最佳答案

首先,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描述了执行此操作的各种方法

关于c# - 为调用方法“Boolean Equals”提供的参数数量不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32713431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com