vendredi 7 avril 2017

How can I achieve to implement the below query without the if statement (SUM/AVG)?

  if (_operationType.Equals(ReportAggregationOperation.Sum))
        {
            return query
                .GroupBy(g => new
                {
                    Day = TestableDbFunctions.TruncateTime(groupBy.Invoke(g))
                })
                .Select(x => new
                {
                    Key = x.Key.Day.Value.ToString("yyyyMMdd"),
                    Value = (int?) x.Sum(operationProperty.Compile()),
                }).ToDictionary(t => t.Key, t => t.Value);
        }
        else
        {
            return query
               .GroupBy(g => new
               {
                   Day = DbFunctions.TruncateTime(groupBy.Invoke(g))
               })
               .Select(
                x => new
                {
                    Key = x.Key.Day.ToString("yyyyMMdd"),
                    Value = (int?) x.Average(operationProperty.Compile()),
                }).ToDictionary(t => t.Key, t => t.Value);
        }

I'm using an IOC container for creating instances like dailyaggreagtor/monthlyaggregator... But I wasn't able to build this group by expression, or to apply the right design pattern to eliminate the above if statement.

Aucun commentaire:

Enregistrer un commentaire