简体   繁体   English

如何通过ASP.NET中的C#从数据库的列中获取平均值?

[英]How to get average value from a column in a database through C# in ASP.NET?

I'm very new to C# and working in ASP.NET, and I'm supposed to get all the information from a table in a local database that I have connected with in Visual Studio. 我是C#的新手,并且在ASP.NET中工作,我应该从Visual Studio中与之连接的本地数据库的表中获取所有信息。 All I really know is how to connect and how to get the whole table in a grid view, but I would in the end of the grid view, or maybe in some other way to show the medium value for each column. 我真正知道的是如何连接以及如何在网格视图中获取整个表,但是我会在网格视图的末尾,或者也许以其他方式来显示每一列的中间值。

It's the answers from a survey built on radiobuttons with the values 1 to 5. So I want to get the medium from each question. 这是从基于1到5的单选按钮进行的​​调查得出的答案。因此,我想从每个问题中获取答案。 In my database I only have one column for id and one column each for every question. 在我的数据库中,我只有一列ID,每个问题一列。

Right now I have this: 现在我有这个:

The code in result.aspx: result.aspx中的代码:

<asp:GridView ID="grdResult" CssClass="grid" runat="server">
</asp:GridView>

The code in result.aspx.cs: result.aspx.cs中的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Uppgift_3
{
 public partial class result : System.Web.UI.Page
 {
    protected void Page_Load(object sender, EventArgs e)
    {
        using (resultatEntities datakoppling = new resultatEntities()) 
        {
            var answers = from survey in datakoppling.surveyAnswers
                          select survey;

            grdResult.DataSource = answers;

            grdResult.DataBind();
        }
    }
 }
}

When i put things in to the database in default.aspx: 当我将内容放入default.aspx的数据库中时:

    protected void saveSurvey_Click(object sender, EventArgs e)
    {

        using (resultatEntities datakoppling = new resultatEntities())
        {

            var saveSurvey = new surveyAnswers();

            saveSurvey.Question1 = RadioButtonQuestion1.SelectedItem.ToString();
            saveSurvey.Question2 = RadioButtonQuestion2.SelectedItem.ToString();
            saveSurvey.Question3 = RadioButtonQuestion3.SelectedItem.ToString();
            saveSurvey.Question4 = RadioButtonQuestion4.SelectedItem.ToString();
            saveSurvey.Question5 = RadioButtonQuestion5.SelectedItem.ToString();
            saveSurvey.Question6 = RadioButtonQuestion6.SelectedItem.ToString();
            saveSurvey.Question7 = RadioButtonQuestion7.SelectedItem.ToString();
            saveSurvey.Question8 = RadioButtonQuestion8.SelectedItem.ToString();
            saveSurvey.Question9 = RadioButtonQuestion9.SelectedItem.ToString();
            saveSurvey.Question10 = RadioButtonQuestion10.SelectedItem.ToString();

            datakoppling.surveyAnswers.AddObject(saveSurvey);
            datakoppling.SaveChanges();

        }
    }
}
}

If you mean that you want the mean value (or average), that's pretty easy: 如果您想要平均值 (或平均值),那很简单:

var mean = datakoppling.surveyAnswers.Average(s => s.Value);

If you mean that you want the median value (the value such that half of the values in the set are above and half of the values in the set are below), it takes a bit more work, but you can still do it. 如果您想要的是中间值(该值使得集合中一半的值位于上方,而集合中一半的值位于下方),则需要做更多的工作,但是您仍然可以这样做。 Try this: 尝试这个:

var count = datakoppling.surveyAnswers.Count();
var midPoint = count / 2;
double medianValue;
if (count % 2 = 0)
{
    medianValue = datakoppling.surveyAnswers
                              .OrderBy(s => s.Value)
                              .Take(midPoint + 1)
                              .Skip(midPoint - 1)
                              .Average(s => s.Value);
}
else
{
    medianValue = datakoppling.surveyAnswers
                              .OrderBy(s => s.Value)
                              .Select(s => s.Value)
                              .Take(midPoint + 1)
                              .Last();
}

Here I assumed the property Value was what you want to average, of course you can tweak this code as needed to fit your particular case. 在这里,我假设属性Value是您想要平均的值,当然您可以根据需要调整此代码以适合您的特定情况。


Update : 更新
As a side note, you can calculate multiple averages with a single query like this: 附带一提,您可以使用单个查询来计算多个平均值,如下所示:

var averages = 
    from s in datakoppling.surveyAnswers
    group s by 0 into g
    select new 
    {
        Value1 = g.Average(s => s.Value1),
        Value2 = g.Average(s => s.Value2),
        Value3 = g.Average(s => s.Value3),
        ...
    };

To use this to DataBind to your grid, simply do: 要将其用于DataBind到网格,只需执行以下操作:

protected void Page_Load(object sender, EventArgs e)
{
    using (resultatEntities datakoppling = new resultatEntities()) 
    {
        var averages = 
            from s in datakoppling.surveyAnswers
            group s by 0 into g
            select new 
            {
                question1 = g.Average(s => s.question1),
                question2 = g.Average(s => s.question2),
                question3 = g.Average(s => s.question3),
                ...
            };

        grdResult.DataSource = averages;

        grdResult.DataBind();
    }
}

If you want both the answers and the averages in the grid use: 如果您想要答案又希望得到网格中的平均值,请使用:

protected void Page_Load(object sender, EventArgs e)
{
    using (resultatEntities datakoppling = new resultatEntities()) 
    {
        var answers = 
            from s in datakoppling.surveyAnswers
            select new 
            {
                id = s.Id,
                question1 = (double)s.question1,
                question2 = (double)s.question2,
                question3 = (double)s.question3,
                ...
            };
        var averages = 
            from s in datakoppling.surveyAnswers
            group s by 0 into g
            select 
            {
                id = 0,
                question1 = g.Average(s => s.question1),
                question2 = g.Average(s => s.question2),
                question3 = g.Average(s => s.question3),
                ...
            };

        grdResult.DataSource = answers.Union(averages);

        grdResult.DataBind();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM