1

I've been searching for a way to make this but I haven't had success. The following is my problem, I have a table that look like this:

ID|CATEGORY|START_DATE |END_DATE 1 T1 2019-08-29 12:00:00 2019-08-29 12:10:00 2 T1 2019-08-30 13:00:00 2019-08-30 13:20:00 3 T2 2019-08-30 14:00:00 2019-08-30 14:10:00 

What I want to do is to GROUP BY Category AND SUM the difference between both dates and get a result like this: T1 --> 30 minutes, t2 --> 10 minutes

This is what I've tried:

foreach (IGrouping<string, TABLE1> group in result) { series.Add(new PieSerie("SerieName", group.AsEnumerable().Sum(x => (x.START_DATE- x.END_DATE).Value.Seconds ))); } 

But it doesn't work. Every single result I get is '0' instead of the total of minutes.

1

1 Answer 1

2

If you subtract (as DateTime) 2019-08-30 14:00:00 from 2019-08-30 14:10:00, you get (as a TimeSpan) 00:10:00, which is to say "zero hours, 10 minutes, and zero seconds". The "and zero seconds" is a clue why you're getting 0 from .Seconds. You probably want .TotalSeconds (which will be 600), or just keep the entire TimeSpan itself.

Sign up to request clarification or add additional context in comments.

1 Comment

I was conffused, I'm new at lambda and linq queries, and I was always worked with DateTime, never with TimeSpan. that's why I was trying to get seconds like that. But TotalSeconds Solved the problem. Thanks !!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.