The LINQ expression could not be translated about sum of Property TimeSpan

Viewed 517

I have a table named CourseEpisode that have different property , one of them is TimeSpan

   public TimeSpan time{ get; set; }

when i trying to get sum of Episod Time with this code :

    var TotalTime = new TimeSpan(c.CourseEpisods.Sum(e => e.time.Ticks))

when i run my project i face with this Error :

InvalidOperationException: The LINQ expression '(EntityShaperExpression: EntityType: CourseEpisod ValueBufferExpression: (ProjectionBindingExpression: EmptyProjectionMember) IsNullable: False ).Time.Ticks' 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(),ToList(), or ToListAsync()

please help me

1 Answers

here is an example

void Main()
{   
    var table = new List<TimeSpan> 
                {
                    new TimeSpan(1,0,0),
                    new TimeSpan(0,30,0),                   
                    new TimeSpan(2,0,0),
                    new TimeSpan(0,0,10)
                };
                
    
    TimeSpan total = TimeSpan.Zero;
    table.ForEach(t => total = total.Add(t) );  
    
    Console.WriteLine($"Total Duration Is  = {total.ToString()}");
}

Then you get

Total Duration Is  = 03:30:10
Related