efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Query: Timespan.TotalMinutes is not translated to server

Open jfcaldeira opened this issue 6 years ago • 5 comments

I just upgrade to EF 3 and one of my queries that used to work, gives an exception now

 ProductionRecords = _context.ProductionRecords
          .Where(r => r.DataCriacao.Date == DateTime.Now.Date)
            .Select(pr => new ProductionRecordViewModel
            {
                Id = pr.Id,
                Operador = pr.Operador,
                DataCriacao = pr.DataCriacao,
                Celula = pr.Celula.Name,
                Turno = pr.Turno.Name,
                TotalPecasSemDefeito = pr.ReferenceRecords.Sum(c => c.Quantity),
                TotalPecasComDefeito = pr.DefectRecords.Sum(c => c.Quantidade),
                TotalTempoParado = pr.StopRecords.Sum(c => Convert.ToInt32(c.Duration.TotalMinutes)),
            })
          .AsNoTracking()
          .ToList();

One of the solutions was to add AsEnumerable before select but that will reduce performance a lot. How can i return the sum of timespan minutes without evaluating on client side?

Here is the exception that i get

InvalidOperationException: The LINQ expression '(EntityShaperExpression: EntityType: StopRecord ValueBufferExpression: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: False ).Duration.TotalMinutes' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.

here are the models

```
public class ProductionRecord
{
    public ProductionRecord()
    {
        DataCriacao = DateTime.Now;
        DateStamp = DateTime.Now.Date;
    }

    [Display(Name = "Registo")]
    public int Id { get; set; }
    [Display(Name = "Operador")]
    public int Operador { get; set; }
    [Display(Name = "Criado Em")]

    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
    public DateTime DataCriacao { get; set; }        
    [Column(TypeName = "Date")]
    public DateTime DateStamp { get; set; }
    public int CelulaId { get; set; }
    public Celula Celula { get; set; }
    public int TurnoId { get; set; }
    public Turno Turno { get; set; }

    public virtual ICollection<CheckListRecord> CheckListRecords { get; set; }
    public virtual ICollection<StopRecord> StopRecords { get; set; }
    public virtual ICollection<ReferenceRecord> ReferenceRecords { get; set; }
    public virtual ICollection<DefectRecord> DefectRecords { get; set; }
}

public class StopRecord
{
    public int Id { get; set; }

    [Required]
    [DataType(DataType.Time)]   
    [Display(Name = "Tempo (min)")]
    public TimeSpan Duration { get; set; }

    public bool IsDeleted { get; set; }

    [MaxLength(200)]
    [StringLength(200)]
    public string Description { get; set; }

    [Required]
    [Display(Name = "Paragem")]
    public int StopCodeId { get; set; }
    public StopCode StopCode { get; set; }
    public int ProductionRecordId { get; set; }
    public ProductionRecord ProductionRecord { get; set; }
}

jfcaldeira avatar Dec 19 '19 00:12 jfcaldeira

@jfcaldeira - Can you also post error message & stack trace and your model classes?

smitpatel avatar Dec 19 '19 01:12 smitpatel

@jfcaldeira - Can you also post error message & stack trace and your model classes?

Hi i have edited with the error message and the models

jfcaldeira avatar Dec 19 '19 08:12 jfcaldeira

Looking into this. I want to write a test that essentially just reproduces this bug so that we can make sure it stays fixed. I believe that to reproduce we just need to try to sum TimeSpan.totalMinutes values of a nested collection on an entity, and make sure this returns the correct result. I'm new to the codebase and am having trouble locating where such a test would fit in the test code. I was looking into the BuiltinDataTypesTestBase class, would this be a good avenue to look at? Any help would be greatly appreciated!

cowmanjoe avatar Jan 13 '20 02:01 cowmanjoe

Looks like #19617 will address this issue for Sqlite. Still need more changes to work on other platforms

cowmanjoe avatar Jan 21 '20 03:01 cowmanjoe

Subset of #19632

bricelam avatar Jan 24 '20 23:01 bricelam