【问题标题】:EF Core using Let in query throw "Argument types do not match" exceptionEF Core 在查询中使用 Let 引发“参数类型不匹配”异常
【发布时间】:2018-09-10 03:19:20
【问题描述】:

我使用的是 EF Core 2.0.1。我正在尝试获取最后一个订单数据以及用户信息来填充 UserDto,如下所示,

(from u in DbContext.User.Where(u => u.UserId == userId)
let last = u.Orders.LastOrDefault()
select new UserDto
{
    Id = u.Id,
    Name = u.Name,
    LastOrderId = (last == null ? null : last.Id),
    LastOrderDate = (last == null ? null : last.Date)
}

这会引发以下异常。我该如何解决这个问题?

at System.Linq.Expressions.Expression.Condition(Expression test, Expression ifTrue, Expression ifFalse, Type type)
at System.Linq.Expressions.ConditionalExpression.Update(Expression test, Expression ifTrue, Expression ifFalse)
at System.Linq.Expressions.ExpressionVisitor.VisitConditional(ConditionalExpression node)
at System.Linq.Expressions.ConditionalExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.Visit(Expression expression)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberAssignment(MemberAssignment node)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)
at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection`1 nodes, Func`2 elementVisitor)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.VisitMemberInit(MemberInitExpression memberInitExpression)
at System.Linq.Expressions.MemberInitExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.RelationalProjectionExpressionVisitor.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel)
at Remotion.Linq.Clauses.SelectClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel)
at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateAsyncQueryExecutor[TResult](QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileAsyncQuery[TResult](QueryModel queryModel)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileAsyncQueryCore[TResult](Expression query, INodeTypeProvider nodeTypeProvider, IDatabase database)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass24_0`1.<CompileAsyncQuery>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddAsyncQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileAsyncQuery[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.System.Collections.Generic.IAsyncEnumerable<TResult>.GetEnumerator()
at System.Linq.AsyncEnumerable.<Aggregate_>d__6`3.MoveNext()

【问题讨论】:

  • 检查 LastOrderId、last.Id、LastOrderDate 和 last.Date 的类型。
  • LastOrderId, last.Id 是 int,LastOrderDate 和 last.Date 是日期时间。如果存在类型不匹配,编译器会在运行前显示它们。不是吗?

标签: c# entity-framework linq entity-framework-core


【解决方案1】:

您的查询没有任何问题。但是,尽管 EF Core 尝试翻译 Last / LastOrDefault 方法(EF6 简单地抛出 NotSupportedException),但显然它仍然存在问题。

问题是Last / LastOrDefault 没有完全定义为本质上无序的数据库原始序列。

很快,避免Last / LastOrDefault。请改用OrderByDescending + First / FirstOrDefault

以下作品:

let last = u.Orders.OrderByDescending(o => o.Date).FirstOrDefault()

只需使用适合您的数据逻辑的排序列来确定应该是“第一个”、“最后一个”等。

其实这也有效:

let last = u.Orders.OrderBy(o => o.Date).LastOrDefault()

但 EF Core 会尝试将其转换为上述构造,因此比显式方式更容易出现 EF Core 实现错误。

【讨论】:

  • First / FirstOrDefault 方法解决了这个问题。谢谢
猜你喜欢
  • 2021-11-18
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
  • 2021-08-09
相关资源
最近更新 更多