Skip to content

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

Closed as duplicate

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

Activity

  1. smitpatel commented on Dec 19, 2019

    @smitpatel
    Contributor

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

  2. jfcaldeira commented on Dec 19, 2019

    @jfcaldeira
    Author

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

    Hi i have edited with the error message and the models

  3. changed the title [-]Problem with using sum on timespan[/-] [+]Query: Timespan.TotalMinutes is not translated to server[/+] on Dec 19, 2019
  4. added this to the Backlog milestone on Dec 23, 2019
  5. added
    good first issue
    on Dec 23, 2019
  6. cowmanjoe commented on Jan 13, 2020

    @cowmanjoe

    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!

  7. cowmanjoe commented on Jan 21, 2020

    @cowmanjoe

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

  8. bricelam commented on Jan 24, 2020

    @bricelam
    Contributor

    Subset of #19632

  9. removed this from the Backlog milestone on Jan 24, 2020
  10. removed
    good first issue
    on Oct 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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