Entity Framework - How to get Standard Deviation using GroupBy?

1.2k views Asked by At

I'm using entity framework 5, code first fluent API. I've built a simple query with a GroupBy for aggregation:

                        var query = context.Table1
                                    .GroupBy(g => new
                                    {
                                        Prop1= g.Prop1,
                                        Prop2 = g.Prop2
                                    })
                                    .Select(s => new
                                    {
                                        Prop1= s.Key.Prop1,
                                        Prop2= s.Key.Prop2,
                                        Avg = s.Average(m => m.Minutes),
                                        Min = s.Min(m => m.Minutes),
                                        Max = s.Max(m => m.Minutes),
                                        StdDev = //How to get standard deviation of Minutes here?
                                    })
                                    .ToList();

Along with other aggregation (average, min, max), how can I get standard deviation of Minutes in my select? I've heard there is a canonical function for StdDev but have been unable to figure out how to apply it in this case. Any help or a nudge in the right direction would be greatly appreciated!

1

There are 1 answers

0
jjj On BEST ANSWER

You should be able to use EntityFunctions.StandardDeviation in System.Data.Objects like this:

StdDev = EntityFunctions.StandardDeviation(s.Select(m => m.Minutes))

(If you were using EF 6, it would be System.Data.Entity.DbFunctions.StandardDeviation())