dotnet/efcore

Nullable DateTimeOffset .Value is executed locally in F#

Open

#14,013 创建于 2018年11月26日

在 GitHub 查看
 (10 评论) (1 反应) (0 负责人)C# (14,651 star) (3,376 fork)batch import
area-querycustomer-reportedhelp wanted

描述

I can't seem to achieve the same semantic between a C# query and a F# query. Do I need to do something extra to produce the same behavior as C#?

Here is the record type and db context that I am using:

// From a .NET Standard C# library
public class BlogRecord
{
    public int Id { get; set; }
    public DateTimeOffset? CreationDate { get; set; }
}

public class BloggingContext : DbContext
{
    public BloggingContext(DbContextOptions<BloggingContext> options)  : base(options) { }
    public DbSet<BlogRecord> Blogs { get; set; }
}

In C#, the following query:

var blogs = dbContext.Blogs
    .Where(x => x.CreationDate.HasValue && x.CreationDate.Value > date)
    .ToList();

Results in the expected SQL query:

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (11ms) [Parameters=[@__date_0='?'], CommandType='Text', CommandTimeout='30']
      SELECT "x"."Id", "x"."CreationDate", "x"."Serial", "x"."Url"
      FROM "Blogs" AS "x"
      WHERE "x"."CreationDate" IS NOT NULL AND ("x"."CreationDate" > @__date_0)

But in F#, the (kind of) same query:

let blogs = dbContext.Blogs
    .Where(fun (x: BlogRecord) -> x.CreationDate <> Nullable() && x.CreationDate.Value > date)
    .ToList()

Results in a warning whereby the .Value can't be translated and the function is introducing a copyOfStruct:

warn: Microsoft.EntityFrameworkCore.Query[20500]
      The LINQ expression 'where (copyOfStruct => Convert(copyOfStruct, DateTimeOffset).Invoke([x].CreationDate) > 26/11/2018 21:58:41 +00:00)' could not be translated and will be evaluated locally.

info: Microsoft.EntityFrameworkCore.Database.Command[20101]
      Executed DbCommand (79ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [x].[Id], [x].[CreationDate], [x].[Serial], [x].[Url]
      FROM [Blogs] AS [x]
      WHERE [x].[CreationDate] IS NOT NULL

Further technical details

EF Core version: 2.1.4 Database Provider: Microsoft.EntityFrameworkCore.SqlServer and Sqlite

贡献者指南