1

我对 C# 非常陌生,并且在 ASP.NET 中工作,我应该从我在 Visual Studio 中连接的本地数据库中的表中获取所有信息。我真正知道的是如何连接以及如何在网格视图中获取整个表格,但我会在网格视图的末尾,或者以其他方式显示每列的中间值。

这是基于单选按钮(值为 1 到 5)构建的调查的答案。所以我想从每个问题中获取媒介。在我的数据库中,我只有一列 id 和一列每个问题。

现在我有这个:

result.aspx 中的代码:

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

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

当我将东西放入 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();

        }
    }
}
}
4

1 回答 1

2

如果你的意思是你想要平均值(或平均值),那很容易:

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

如果你的意思是你想要值(这个值使得集合中一半的值在上面,而集合中的一半值在下面),它需要更多的工作,但你仍然可以做到。尝试这个:

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

在这里,我假设该属性Value是您想要平均的,当然您可以根据需要调整此代码以适合您的特定情况。


更新
作为旁注,您可以使用单个查询计算多个平均值,如下所示:

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),
        ...
    };

要将其用于 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();
    }
}

如果您想要网格的答案和平均值,请使用:

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();
    }
}
于 2013-06-02T18:38:57.877 回答