C# 查找并存储多次更新的十进制变量的最大/最小值

C# 查找并存储多次更新的十进制变量的最大/最小值,c#,asp.net,C#,Asp.net,在一个应用程序会话中,我多次计算十进制netscore,每次变量发生变化时,我都会比较变量,以确定三个最大值和三个最小值 我目前正在会话状态中存储三个最大值和三个最小值。下面的代码并不总是正常工作-例如,如果我按以下顺序将netscore设置为以下值:57,64,27,56,45,53,62,42,64,40,53,71,57,54,50,它将返回71,71,64,作为最大的三个值。我希望看到71,64,64 有人能认出我做错了什么吗 Session["top1"] = "0"; Session

在一个应用程序会话中,我多次计算十进制
netscore
,每次变量发生变化时,我都会比较变量,以确定三个最大值和三个最小值

我目前正在会话状态中存储三个最大值和三个最小值。下面的代码并不总是正常工作-例如,如果我按以下顺序将
netscore
设置为以下值:
57,64,27,56,45,53,62,42,64,40,53,71,57,54,50
,它将返回
71,71,64
,作为最大的三个值。我希望看到
71,64,64

有人能认出我做错了什么吗

Session["top1"] = "0";
Session["top2"] = "0";
Session["top3"] = "0";
Session["bottom1"] = "100";
Session["bottom2"] = "100";
Session["bottom3"] = "100";

//Fetch values required to calculate netscore 
SqlCommand fivecmd = new SqlCommand(query5, mySLTConnection);            
var fives = Convert.ToSingle(fivecmd.ExecuteScalar());
SqlCommand fourcmd = new SqlCommand(query4, mySLTConnection);
var fours = Convert.ToSingle(fourcmd.ExecuteScalar());
SqlCommand threecmd = new SqlCommand(query3, mySLTConnection);
var threes = Convert.ToSingle(fourcmd.ExecuteScalar());
SqlCommand twocmd = new SqlCommand(query2, mySLTConnection);
var twos = Convert.ToSingle(twocmd.ExecuteScalar());
SqlCommand onecmd = new SqlCommand(query1, mySLTConnection);
var ones = Convert.ToSingle(onecmd.ExecuteScalar());
mySLTConnection.Close();

//Get total count
var total = fives + fours + threes + twos + ones;

//Get net score
var netscore = Convert.ToDecimal((((fives + fours) - (twos + ones)) / total)*100); 
netscore = Math.Round(netscore,0);


//Begin comparing netscore to stored top/bottom values
if (netscore > Convert.ToDecimal(Session["top1"]))
{
    Session["top3"] = Session["top2"];
    Session["top2"] = Session["top1"];
    Session["top1"] = netscore;
    Session["top1q"] = question.ToUpper();

}
else if (netscore > Convert.ToDecimal(Session["top2"]))
{
    Session["top3"] = Session["top2"];
    Session["top2"] = netscore;
    Session["top2q"] = question.ToUpper();
}
else if (netscore > Convert.ToDecimal(Session["top3"]))
{
    Session["top3"] = netscore;
    Session["top3q"] = question.ToUpper();
}
else if (netscore < Convert.ToDecimal(Session["bottom1"]))
{
    Session["bottom3"] = Session["bottom2"];
    Session["bottom2"] = Session["bottom1"];
    Session["bottom1"] = netscore;
    Session["bottom1q"] = question.ToUpper();
}

else if (netscore < Convert.ToDecimal(Session["bottom2"]))
{
    Session["bottom3"] = Session["bottom2"];
    Session["bottom2"] = netscore;
    Session["bottom2q"] = question.ToUpper();
}

else if (netscore < Convert.ToDecimal(Session["bottom3"]))
{
    Session["bottom3"] = netscore;
    Session["bottom3q"] = question.ToUpper();
}

lblSVal1.Text = Session["top1"].ToString();
lblSVal2.Text = Session["top2"].ToString();
lblSVal3.Text = Session["top3"].ToString();
会话[“top1”]=“0”;
会话[“top2”]=“0”;
会话[“top3”]=“0”;
会话[“bottom1”]=“100”;
会话[“bottom2”]=“100”;
会话[“bottom3”]=“100”;
//获取计算netscore所需的值
SqlCommand fivecmd=新的SqlCommand(query5,myslconnection);
var fives=Convert.ToSingle(fivecmd.ExecuteScalar());
SqlCommand fourcmd=新的SqlCommand(query4,myslconnection);
var fours=Convert.ToSingle(fourcmd.ExecuteScalar());
SqlCommand threecmd=新的SqlCommand(query3,myslconnection);
var threes=Convert.ToSingle(fourcmd.ExecuteScalar());
SqlCommand twocmd=新的SqlCommand(query2,myslconnection);
var twos=Convert.ToSingle(twocmd.ExecuteScalar());
SqlCommand onecmd=新的SqlCommand(query1,myslconnection);
var ones=Convert.ToSingle(onecmd.ExecuteScalar());
myslconnection.Close();
//获取总计数
var总计=五+四+三+二+一;
//得净分
var netscore=Convert.ToDecimal(((五加四)-(两加一))/total)*100;
netscore=Math.Round(netscore,0);
//开始将netscore与存储的顶部/底部值进行比较
if(netscore>Convert.ToDecimal(会话[“top1”]))
{
会话[“top3”]=会话[“top2”];
会话[“top2”]=会话[“top1”];
会话[“top1”]=netscore;
会话[“top1q”]=question.ToUpper();
}
else if(netscore>Convert.ToDecimal(会话[“top2”]))
{
会话[“top3”]=会话[“top2”];
会话[“top2”]=netscore;
会话[“top2q”]=question.ToUpper();
}
else if(netscore>Convert.ToDecimal(会话[“top3”]))
{
会话[“top3”]=netscore;
会话[“top3q”]=question.ToUpper();
}
else if(netscore
一个快速但不是很优雅的解决方案是使用以下模式:

if (netscore >= Convert.ToDecimal(Session["top1"]))
{
   if (netscore > Convert.ToDecimal(Session["top1"])) {
      Session["top3"] = Session["top2"];
      Session["top2"] = Session["top1"];
      Session["top1"] = netscore;
      Session["top1q"] = question.ToUpper();  
   }
   // If it's not a new top value, it will be ignored
} 
// ... and so on ...
基本上回答了您的问题,但我注意到您还存储了一些相关的问题数据。首先,我建议创建一个类来存储相关数据

class NetScore {
    public decimal NetScore;
    public string Message;
}
然后,这里是一个生成netscore的方法(添加参数以便正确构建查询)。另外,由于您没有详细说明消息是如何加载的,我想您可以理解这一点

public NetScore GetNetScore() {
    //Fetch values required to calculate netscore 
    SqlCommand fivecmd = new SqlCommand(query5, mySLTConnection);            
    var fives = Convert.ToSingle(fivecmd.ExecuteScalar());
    SqlCommand fourcmd = new SqlCommand(query4, mySLTConnection);
    var fours = Convert.ToSingle(fourcmd.ExecuteScalar());
    SqlCommand threecmd = new SqlCommand(query3, mySLTConnection);
    var threes = Convert.ToSingle(fourcmd.ExecuteScalar());
    SqlCommand twocmd = new SqlCommand(query2, mySLTConnection);
    var twos = Convert.ToSingle(twocmd.ExecuteScalar());
    SqlCommand onecmd = new SqlCommand(query1, mySLTConnection);
    var ones = Convert.ToSingle(onecmd.ExecuteScalar());
    mySLTConnection.Close();

    //Get total count
    var total = fives + fours + threes + twos + ones;

    //Get net score
    return new NetScore() {
        NetScore = Math.Round(Convert.ToDecimal((((fives + fours) - (twos + ones)) / total)*100), 0),
        Message = //however you get the message...
    }
}
对于主代码,可以将它们全部加载到列表中。因为你没有详细说明你是如何得到所有的净分数的,我只是为你需要做的任何循环设置了一个foreach。这基本上会调用上面的方法来将message/netscore对象生成到一个列表中。一旦它进入列表,就很容易通过linq获得前三名和后三名

List<NetScore> netScores = new List<NetScore>();

// load all the net scores and their messages
foreach(... in ...) {
    netScore.Add(GetNetScore());
}

// get the top3 and bottom3
IEnumerable<NetScore> top3 = netScores.OrderByDescending(s => s.NetScore).Take(3);
IEnumerable<NetScore> bottom3 = netScores.OrderBy(s => s.NetScore).Take(3);

如果新值大于top3,但不是top1@ohmusamatop2和top3重复该模式(对于bottom1、2、3,我也不知道为什么需要嵌套的If语句。是否需要在会话中存储数据?奇怪的是,我在控制台中运行您的代码,它显示top1、2和3是71、64、64。请看……我没有存储在会话中,只是在需要跨页面共享时才这样做,我没有。好的,我将提供一个答案。)t将有一些空白供您填写,但它应该非常简单。不确定这是否相关,但在您粘贴的代码中,
threecmd
没有被调用-
fourcmd
调用了两次。这也将副本粘贴到了@ohnusama答案中。非常感谢您的回复。一切看起来都很好,但我得到了以下e错误:
错误306可访问性不一致:返回类型“NetScore”的可访问性不如方法“GetNetScore”(string,string,string)“
我是否将
NetScore
类放置在错误的位置?通过将NetScore类设置为public可以解决此问题:
public class NetScore
此解决方案工作完美,结果将按预期返回。再次感谢您提供详细、深思熟虑的答案。您帮了大忙!
lblSVal1.Txt = top3.ElementAt(0).NetScore.ToString();
lblSVal2.Txt = top3.ElementAt(1).NetScore.ToString();
lblSVal3.Txt = top3.ElementAt(2).NetScore.ToString();