Return 列值与其他列值条件 LINQ 的标准差
Return Standard Deviation of column values with other column value condition LINQ
我有一个 table、db.Men
,它有三列
- nvarchar "Name"
- 整数"Age"
- nvarchar "Status"
Status
列只有三个值:"Happy"、"Normal" 和 "Bad"。
我需要计算"Happy"或"Normal"年龄的平均值和标准差:
using System.Linq
var ctx = new DataClasses1DataContext();
double? avg = (from n in ctx.men
where n.status == "Happy"
select n.age).Average();
int? sum = (from n in ctx.men
where n.status == "Happy"
select n.age).Sum();
我计算了平均值和总和。如何计算此条件状态下的标准差?
将 Happy
个年龄(或每个 Status
个年龄)粘贴到列表中,然后计算标准偏差。本文作为确定标准偏差的良好过程:
Standard deviation of generic list?
像这样:
var happyAges = ctx.Men.Where(i=>i.status == "Happy")Select(i=>i.age);
var happySd = CalculateStdDev(happyAges);
此外,您可以将标准偏差方法设为静态,并使用一种扩展方法在一个请求中完成所有操作。
我这样做了并且对我有用:
int?[] array = (from a in ctx.men where a.status == "Happy" select a.age).ToArray();
double? AVG = array.Average();
double? SumOfSquaresOfDifferences = array.Select(val => (val - AVG) * (val - AVG)).Sum();
double? SD = Math.Sqrt(Convert.ToDouble(SumOfSquaresOfDifferences) / array.Length);
我有一个 table、db.Men
,它有三列
- nvarchar "Name"
- 整数"Age"
- nvarchar "Status"
Status
列只有三个值:"Happy"、"Normal" 和 "Bad"。
我需要计算"Happy"或"Normal"年龄的平均值和标准差:
using System.Linq
var ctx = new DataClasses1DataContext();
double? avg = (from n in ctx.men
where n.status == "Happy"
select n.age).Average();
int? sum = (from n in ctx.men
where n.status == "Happy"
select n.age).Sum();
我计算了平均值和总和。如何计算此条件状态下的标准差?
将 Happy
个年龄(或每个 Status
个年龄)粘贴到列表中,然后计算标准偏差。本文作为确定标准偏差的良好过程:
Standard deviation of generic list?
像这样:
var happyAges = ctx.Men.Where(i=>i.status == "Happy")Select(i=>i.age);
var happySd = CalculateStdDev(happyAges);
此外,您可以将标准偏差方法设为静态,并使用一种扩展方法在一个请求中完成所有操作。
我这样做了并且对我有用:
int?[] array = (from a in ctx.men where a.status == "Happy" select a.age).ToArray();
double? AVG = array.Average();
double? SumOfSquaresOfDifferences = array.Select(val => (val - AVG) * (val - AVG)).Sum();
double? SD = Math.Sqrt(Convert.ToDouble(SumOfSquaresOfDifferences) / array.Length);