如何获得linq中两个不同列的平均值?

时间:2017-10-02 09:07:57

标签: c# linq

我是linq的新手,对于总体事情有点困惑。在下文中,我需要取m_corected_avm_wind_speed的平均值。你能给我一个提示吗?

  var ChartDetails = (from c in db.StatisticsMonthlies
                            join d in db.MasterDatas
                            on c.m_turbine_id equals d.m_turbine_id
                            join s in db.UsersTurbines
                            on d.turbine_id equals s.tur_id
                            where c.m_date > lastyeardate && s.user_id == useID
                            select new MonthlyDailyStatsOfTurbines
                            {
                                m_energy_prod = (int)c.m_energy_prod,
                                turbine_name = d.turbine_name,
                                m_wind_speed = Math.Round((Decimal) c.m_wind_speed),
                                m_wind_speed = (int)c.m_wind_speed,
                                m_date = c.m_date,                                  
                                m_corrected_av = c.m_corrected_av,

                            }).ToList().OrderBy(m => m.m_date).Select(o => new MonthlyDailyStatsOfTurbines
                            {
                                m_energy_prod = (int)o.m_energy_prod,
                                turbine_name = o.turbine_name,
                                //m_wind_speed = Math.Round((Decimal) c.m_wind_speed),
                                m_wind_speed = (int)o.m_wind_speed,
                                m_date = o.m_date,
                                m_data_completeness = o.m_data_completeness,
                                m_corrected_av = o.m_corrected_av,
                                date_test = o.m_date.ToString("MMMM yyyy")

                            }).ToList();

1 个答案:

答案 0 :(得分:2)

您的问题可以通过不同方式理解。如果你想要机器人列表中av的平均值和平均风速,你可以去做某事。像:

var avg_corrected_av = ChartDetails.Average(x => x.m_corected_av); 
var avg_wind_speed = ChartDetails.Average(x => x.m_wind_speed);

如果您想要结果集中每个项目的平均值和风速的列表,您可以这样做:

var avg = ChartDetails.Select(x => (x.m_corected_av + x.m_wind_speed)/2);