how to sum TimeSpan elements properly?

Posts   
 
    
etk
User
Posts: 24
Joined: 27-Jul-2010
# Posted on: 04-Jun-2013 15:14:36   

Hello, I've tried to sum the TimeSpan elements (element.timePeriod here) using the Aggregate:

totTime = grp.Aggregate( TimeSpan.Zero, ( sum, element ) => sum.Add( element.timePeriod ) )

but SD.LLBLGen.Pro.LinqSupportClasses.NET35 raised the exception: 'Aggregate' isn't supported in this Linq provider. Then I found suggestions here and rewrited the expression to:

totTime = TimeSpan.FromSeconds( grp.Sum( x => x.timePeriod.Value.TotalSeconds ) )

Is it the proper approach? If not - can I avoid the conversion TotalSeconds/FromSeconds?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Jun-2013 20:08:47   

This is a fine approach.

can I avoid the conversion TotalSeconds/FromSeconds?

What exactly that you need here?

etk
User
Posts: 24
Joined: 27-Jul-2010
# Posted on: 05-Jun-2013 08:56:06   

I want to sum all the timePeriods (of type TimeSpan) and get the result of the same type or as a DatTime value. BTW running the query throws a ORMQueryConstructionException exception with the message: "No known database function mapping found for property 'TotalSeconds'". That makes me wonder if the conversion TotalSeconds/FromSeconds is proper here.

The project is based on LLBLGen 2.6 (RuntimeBuild="08292012", RuntimeVersion="2.6.0.0"). The query is performed against MySQL 5.1 server, using Devart dotConnect 7.6

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 05-Jun-2013 18:59:57   

Is this a Linq2Objects, or a Linq2Sql right?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39912
Joined: 17-Aug-2003
# Posted on: 05-Jun-2013 20:33:21   

IMHO you don't need aggregate, you can just do query.Sum(predicate) which calculates the sum value.. ?

Frans Bouma | Lead developer LLBLGen Pro
etk
User
Posts: 24
Joined: 27-Jul-2010
# Posted on: 06-Jun-2013 09:12:08   

@Otis, I just want to use the Sum() but - look at the query (a.OpEnd and a.OpBegin are of DateTime):

var query =
    from m in Machines
    join a in MaintOps on m.Id equals a.MachineId
    orderby m.ProdLine, m.Name
    select new {m.ProdLine, m.Name, period = a.OpEnd - a.OpBegin, /*...*/} into ma
    group ma by new {ma.ProdLine, ma.Name} into gma
    select new {
        line = gma.Key.ProdLine,
        name = gma.Key.Name,
        /*...*/
        totTime = gma.Sum(x => x.period.Value.TotalSeconds)
    };
  • if I apply the x.period.Value.TotalSeconds as above I got ORMQueryConstructionException ("No known database function mapping found for property 'TotalSeconds'"). I cannot use the period.Value directly as the Sum() doesn't support TimeSpan.
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 06-Jun-2013 20:50:49   

Instead of projecting to an anonymous type, you can define a class, with properties of specific types, and project to this class.

instead of:

select new {m.ProdLine, m.Name, period = a.OpEnd - a.OpBegin, /*...*/} into ma

do:

select new MYClass(){ProdLine = m.ProdLine, Name = m.Name, Period = a.OpEnd - a.OpBegin, /*...*/} into ma