1

I found this post and I need something similar

stackoverflow.com/questions/41282053/groupby-multiple-date-properties-by-month-and-year-in-linq/43112774#43112774

I need to group by month and year, but at the same time I need a property of the main element, which I can not access

var departments = stops 
    .SelectMany(x => new[] { x.InitDate.Month, x.InitDate.Year }
    .Where(dt => dt != null).Select(dt => x.InitDate))
    .GroupBy(dt => new { dt.Month, dt.Year }) 
    .OrderBy(g => g.Key.Month)
    .ThenBy(g => g.Key.Year) 
    .Select(g => new 
    { 
        Key = g.Key.Month, 
        Año = g.Key.Year, 
        Duration = 0, 
        Count = g.Count() 
    });

I would need access to "stops.Duration" but if i do this: .SelectMany(x => new[] { x.InitDate.Month, x.InitDate.Year, x.Duration }

it does not group me by month-year

Can anybody help me?

Sorry for my english and thank you very much

Community
  • 1
  • 1

2 Answers2

0

This code should do:

var departments = stops 
    .Where(stop => stop.InitDate != null)
    .SelectMany(stop => new[] { Month = stop.InitDate.Month, Year = stop.InitDate.Year, Duration = stop.Duration })
    .GroupBy(dt => new { dt.Month, dt.Year }) 
    .OrderBy(g => g.Key.Month)
    .ThenBy(g => g.Key.Year) 
    .Select(g => new 
    { 
        Key = g.Key.Month, 
        Año = g.Key.Year, 
        Duration = g.Sum(v => v.Duration), 
        Count = g.Count() 
    });

It selects the duration, groups on month and year, and uses the sum of the duration from the grouped result.

Patrick Hofman
  • 143,714
  • 19
  • 222
  • 294
  • 2
    You should explain what the problem was - `Where` was applied to the *argument* of `SelectMany`, instead of stops, returning unexpected results – Panagiotis Kanavos Mar 30 '17 at 09:08
  • I have a problem because when I do Año = g.Sum(v => v.Duration) Duration is not recognized. I also think you need a parenthesis ")" – Raúl Berros Mar 30 '17 at 09:17
  • There is also another problem. G is datetime, not stop type. – Raúl Berros Mar 30 '17 at 09:23
  • No. It is an anonymous type. – Patrick Hofman Mar 30 '17 at 09:24
  • If I use this code the first problem is that "stop" does not exist in the current context when it is used in .SelectMany(x => new[] { Month = stop.InitDate.Month, Year = stop.InitDate.Year, Duration = stop.Duration }. I do not know what's the problem – Raúl Berros Mar 30 '17 at 09:34
  • And now? @raul. – Patrick Hofman Mar 30 '17 at 09:54
  • You are welcome. Since you are new, please don't forget to accept the answer that helped you most by checking the check mark before that answer. – Patrick Hofman Mar 30 '17 at 11:27
  • Hi. In the next comment, it's my solution, but Pattrick said that it not group anything. The problem is that if i put .GroupBy(dt => new { dt.Month, dt.Year }) Month and year is not considered because dt is int. Wherefore i wrote .GroupBy(dt => new { Month, Year }). how can I solve that? Thank yo so much – Raúl Berros Apr 03 '17 at 15:27
  • @RaúlBerros Why did you unaccept? This code works fine. – Patrick Hofman Jun 08 '17 at 14:55
  • Because { Key = "g.Key.Month and" "Año = g.Key.Year" } is not reconiced. g.Key is integer (i know that it is anonymous type, but is reconiced how integer). Wherefor in the next comment I put ".GroupBy (dt => new {Month, Year}). But it is not working well – Raúl Berros Jun 09 '17 at 09:52
  • If you still have a problem, can you post the code from the class in `stops`? – Patrick Hofman Jun 19 '17 at 07:13
0
int Month = 0, Year= 0, Duration = 0;
var departments = stops 
    .Where(stop => stop.InitDate != null)
    .SelectMany(stop => new[] { Month = stop.InitDate.Month, Year = stop.InitDate.Year, Duration = stop.Duration })
    .GroupBy(dt => new { Month, Year }) 
    .OrderBy(g => g.Key.Month)
    .ThenBy(g => g.Key.Year) 
    .Select(g => new 
    { 
        Key = g.Key.Month, 
        Año = g.Key.Year, 
        Duration = g.Sum(v => Duration), 
        Count = g.Count() 
    });

For me, this is the final solution

Patrick Hofman
  • 143,714
  • 19
  • 222
  • 294