Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get averages for each day of week with LINQ

I’m new to C# and This is the first time I’m using LINQ queries. I have a Dictionary as Dictionary<(int MeterId, DateTime TimeStamp), float> With data as follows,

<(10411, 12/1/2022 12:30:00 AM), 5700>
<(10411, 13/1/2022 12:30:00 AM), 5200>
<(10412, 12/1/2022 12:30:00 AM), 200>

Then there’s a method to add them to a list with meterId and average value for each day of week. I’m using LINQ queries to calculate the averages and get them to a list.

        public static List<AveragedMeter> CalculateAverageValues(Dictionary<(int MeterId, DateTime TimeStamp), float> groupedMeterValues)
        {
            var lstResult = groupedMeterValues
                .GroupBy(m => new { m.Key.MeterId, m.Key.TimeStamp.DayOfWeek })
                .Select(g => new AveragedMeter
                {
                    MeterId = g.Key.MeterId,
                    AverageMondayValue,
                    AverageTuesdayValue,
                    AverageWednesdayValue,
                    AverageThursdayValue,
                    AverageFridayValue,
                    AverageSaturdayValue,
                    AverageSundayValue
                })
                .ToList();

            return lstResult;
        }

I’m not certain how to get for these average values. My AveragedMeter class is as follows,

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    class AveragedMeter
    {
        public int MeterId { get; set; }
        public float AverageMondayValue { get; set; }
        public float AverageTuesdayValue { get; set; }
        public float AverageWednesdayValue { get; set; }
        public float AverageThursdayValue { get; set; }
        public float AverageFridayValue { get; set; }
        public float AverageSaturdayValue { get; set; }
        public float AverageSundayValue { get; set; }
    }

>Solution :

You could filter the data by DayOfWeek inside your select instead of grouping the dictionary, something like this:

static List<AveragedMeter> CalculateAverageValues(Dictionary<(int MeterId, DateTime TimeStamp), float> groupedMeterValues)
{
    var lstResult = groupedMeterValues
        .GroupBy(m => m.Key.MeterId)
        .Select(g => new AveragedMeter
        {
            MeterId = g.Key,
            AverageMondayValue = g.Where(n => n.Key.TimeStamp.DayOfWeek == DayOfWeek.Monday).Average(n => n.Value),
            AverageTuesdayValue = g.Where(n => n.Key.TimeStamp.DayOfWeek == DayOfWeek.Tuesday).Average(n => n.Value),
            AverageWednesdayValue = g.Where(n => n.Key.TimeStamp.DayOfWeek == DayOfWeek.Wednesday).Average(n => n.Value),
            AverageThursdayValue = g.Where(n => n.Key.TimeStamp.DayOfWeek == DayOfWeek.Thursday).Average(n => n.Value),
            AverageFridayValue = g.Where(n => n.Key.TimeStamp.DayOfWeek == DayOfWeek.Friday).Average(n => n.Value),
            AverageSaturdayValue = g.Where(n => n.Key.TimeStamp.DayOfWeek == DayOfWeek.Saturday).Average(n => n.Value),
            AverageSundayValue = g.Where(n => n.Key.TimeStamp.DayOfWeek == DayOfWeek.Sunday).Average(n => n.Value),         
        })
        .ToList();

    return lstResult;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading