Skip to content

Query: Timespan.TotalMinutes is not translated to server #19360

@jfcaldeira

Description

@jfcaldeira

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; }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions