简体   繁体   中英

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. 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. In my database I only have one column for id and one column each for every question.

Right now I have this:

The code in result.aspx:

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

The code in 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:

    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.


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:

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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