我有一个在版本核心2.0中运行良好的服务方法,可以返回所有可用或很快可用于租赁的产品。
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Rent> Rents { get; set; }
}
public class Rent
{
public int Id { get; set; }
public Product Product { get; set; }
public int ProductId { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public DateTime CreationTime { get; set; }
}
列出以下所有产品的服务:
可用很快出租(其中(EndDate-DateTime.Now).TotalDays&lt; = 10)
服务如下:
public async Task<IEnumerable<Product>> GetProductsForRent()
{
var innerquery = from e in _repository.Rents
group e by e.ProductId into g
select new
{
ProductId = g.Key,
CreationTime = g.Max(r => r.CreationTime)
};
var query = from d in _repository.Products
join h in innerquery on d.Id equals h.ProductId into gj
from e in gj.DefaultIfEmpty()
select d;
var availableProducts = await query.Include(r=>r.Rents).Where(y => !y.Rents.Any()
|| (y.Rents.First().EndDate - DateTime.Now).TotalDays <= 10).ToListAsync();
return availableProducts;
}
此方法在项目升级到core 2.1 form core 2.0
之前一直运行正常我已经检查了EF核心2.1公告,我可以看到下面的注释:
在2.1版之前,在EF Core中,GroupBy LINQ运算符始终是 在记忆中评估。
我不确定这个翻译是否会导致问题,但我收到错误消息:
System.InvalidOperationException:Nullable对象必须具有值。在Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ProjectionShaper.TypedProjectionShaper
3.Shape(QueryContext queryContext, ValueBuffer& valueBuffer) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.CompositeShaper.TypedCompositeShaper
的lambda_method(Closure,QueryContext,ValueBuffer)上。在Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable {.Shape(QueryContext queryContext,ValueBuffer&amp; valueBuffer) {1}} 1.MoveNextCore(CancellationToken cancellationToken)在System.Linq.AsyncEnumerable.AsyncIterator1.AsyncEnumerator.MoveNext(CancellationToken cancellationToken) at System.Linq.AsyncEnumerable.WhereEnumerableAsyncIterator
2.AsyncSelectEnumerator.MoveNext(CancellationToken cancellationToken)at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor1.MoveNext(CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.AsyncSelectEnumerable
1个来源,TAccumulate种子,Func1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken) at System.Linq.AsyncEnumerable.Aggregate_[TSource,TAccumulate,TResult](IAsyncEnumerable
2 resultSelector,CancellationToken cancellationToken)&#39;&#39; in&#39; codelocation&#39;:141行Abp.Threading.InternalAsyncHelper.AwaitTaskWithPostActionAndFinallyAndGetResult [T](任务3 accumulator, Func
1 postAction,Action1 actualReturnValue, Func
1 actualReturnValue,Action`1 finalAction)at&# 39;&#39;在&#39; codelocation&#39; 74.在Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper,ObjectMethodExecutor executor,Object controller,Object [] arguments)
我的问题:有没有办法简化服务查询(最好是在lambda表达式中)以返回所有可用的产品供租用?