Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 无法转换EF Core 3、OrderByDescending和ThenBy Reurn错误 EF核心3.1.8 Npgsql.EntityFrameworkCore.PostgreSQL 3.1.4_Linq_Entity Framework Core_Entity Framework Core 3.1 - Fatal编程技术网

Linq 无法转换EF Core 3、OrderByDescending和ThenBy Reurn错误 EF核心3.1.8 Npgsql.EntityFrameworkCore.PostgreSQL 3.1.4

Linq 无法转换EF Core 3、OrderByDescending和ThenBy Reurn错误 EF核心3.1.8 Npgsql.EntityFrameworkCore.PostgreSQL 3.1.4,linq,entity-framework-core,entity-framework-core-3.1,Linq,Entity Framework Core,Entity Framework Core 3.1,我实现了以下查询,该查询必须按日期和时间重新生成所有注释的组 var commentsOnPage = await _repository.GetAll() .OrderByDescending(c => c.Date.Date) .ThenBy(c => c.Date.TimeOfDay) .Skip(pageSize * pageIndex) .Take

我实现了以下查询,该查询必须按日期和时间重新生成所有注释的组

var commentsOnPage = await _repository.GetAll()
                .OrderByDescending(c => c.Date.Date)
                .ThenBy(c => c.Date.TimeOfDay)
                .Skip(pageSize * pageIndex)
                .Take(pageSize)
                .ToListAsync();



 public class CommentRepository : GenericRepository<Comment>, ICommentRepository
    {
        private readonly AppDbContext _dbContext;

        public CommentRepository(AppDbContext dbContext) : base(dbContext)
        {
            _dbContext = dbContext;
        }

        public IQueryable<Comment> GetAll() => ((AppDbContext)_dbContext).Comments.AsQueryable();

    }
var commentsOnPage=await\u repository.GetAll()
.OrderByDescending(c=>c.Date.Date)
.ThenBy(c=>c.Date.TimeOfDay)
.Skip(页面大小*页面索引)
.Take(页面大小)
.ToListAsync();
公共类CommentRepository:GenericRepository、ICommentRepository
{
私有只读AppDbContextu dbContext;
公共CommentRepository(AppDbContext dbContext):基本(dbContext)
{
_dbContext=dbContext;
}
public IQueryable GetAll()=>((AppDbContext)\u dbContext.Comments.AsQueryable();
}
返回错误:

An unhandled exception has occurred while executing the request.
System.InvalidOperationException: The LINQ expression 'DbSet<Comment>
    .OrderByDescending(c => c.Date.Date)
    .ThenBy(c => c.Date.TimeOfDay)' could not be translated. 
执行请求时发生未处理的异常。
System.InvalidOperationException:LINQ表达式“DbSet”
.OrderByDescending(c=>c.Date.Date)
.ThenBy(c=>c.Date.TimeOfDay)“”无法翻译。

当前EF核心查询翻译的缺陷之一是缺少支持内容的文档。为了让事情变得更困难,允许数据库提供程序向他们决定使用的某些CLR属性/方法添加翻译,因此即使某些LINQ查询为一个数据库提供程序翻译,也可能为另一个数据库提供程序翻译失败

在这种特殊情况下,不支持的成员是
TimeOfDay
。SqlServer提供程序支持它,但Npgsql不支持。至少提供了Npgsql,表明缺少
TimeOfDay
(不支持)

另一方面,Npgsql支持
DateTime
substract操作符(但SqlServer不支持),因此解决Npgsql问题的一种方法是通过从DateTime值中减去日期部分来计算
TimeOfDay

.ThenBy(c => c.Date - c.Date.Date)
.ThenBy(c => c.Date)
对于这种特殊情况,实现目标的另一种方法(应该与任何提供者一起使用)是简单地使用datetime值

.ThenBy(c => c.Date - c.Date.Date)
.ThenBy(c => c.Date)
这是因为
ThenBy
仅对
OrderBy
中的相等值有效。由于您首先是按日期部分(不含时间)订购,因此对于相同的日期,您可以简单地按日期时间订购,这与一天中的时间具有相同的效果