0
我正在製作一個計算學生成績的程序,看看學生是否是一名榮譽學生。問題是隻添加了第一名榮譽學生的名單。屬於第二和第三的其他人不再顯示。我一直試圖弄清楚這個問題已經有將近2天了,我仍然無法找到解決這個問題的方法。添加項目到列表視圖和過濾現有項目
List<int> clsid = new List<int>(){2,3,5};
int g = clsid.Count();
List<int> temp_rank1 = new List<int>();
List<int> temp_rank2 = new List<int>();
List<int> temp_rank3 = new List<int>();
//get the student id
foreach (int fr in clsid)
{
conek.OPEN("select stud_id from class_info where class_id = '"+fr+"' and tmp_gr1 >= 88");
while (conek.reader.Read())
{
temp_rank1.Add(conek.reader.GetInt32(0));
}
conek.CLOSE();
conek.OPEN("select stud_id from class_info where class_id = '" + fr + "' and tmp_gr1 >=85");
while (conek.reader.Read())
{
temp_rank2.Add(conek.reader.GetInt32(0));
}
conek.CLOSE();
conek.OPEN("select stud_id from class_info where class_id = '" + fr + "' and tmp_gr1 >= 83 ");
while (conek.reader.Read())
{
temp_rank3.Add(conek.reader.GetInt32(0));
}
conek.CLOSE();
}
如果同一個student_id的總數等於g的主題總數,則獲取學生ID。
var tmp1 = temp_rank1.GroupBy(n => n).Where(n => n.Count() == g).Select(n => n.Key);
if (tmp1.Count() >= 1)
{
foreach (var gh in tmp1)
{
rank1.Add(gh);
}
}
var tmp2 = temp_rank2.GroupBy(n => n).Where(n => n.Count() == g).Select(n => n.Key);
if (tmp2.Count() >= 1)
{
foreach (var gh in tmp2)
{
rank2.Add(gh);
}
}
var tmp3 = temp_rank3.GroupBy(n => n).Where(n => n.Count() == g).Select(n => n.Key);
if (tmp3.Count() >= 1)
{
foreach (var gh in tmp3)
{
rank3.Add(gh);
}
}
第一個榮譽學生
nye.SubItems.Add("First Honor");
listView1.Items.Add(nye);
int yt = 0;
foreach (int fgd in idr1)
{
if(rcs1[yt] >= 90){
ListViewItem gtb = new ListViewItem();
conek.OPEN("select concat(l_name,', ',f_name,' ',ucase(substring(m_name,1,1)),'.') from students where stud_id = '"+fgd+"' ");
while (conek.reader.Read())
{
gtb.SubItems.Add(conek.reader.GetString(0));
}
conek.CLOSE();
foreach (int gfad in clsid)
{
conek.OPEN("select tmp_gr1 from class_info where class_info.stud_id = '" + fgd + "' and class_id = '" + gfad + "' ");
while (conek.reader.Read())
{
gtb.SubItems.Add(Math.Round(conek.reader.GetDouble(0),2).ToString());
}
conek.CLOSE();
}
gtb.SubItems.Add(Math.Round(rcs1[yt],2).ToString());
yt++;
listView1.Items.AddRange(new ListViewItem[] { gtb });
}
}
第二個榮譽學生
listView1.Items.Add("");
ListViewItem nyek = new ListViewItem();
nyek.SubItems.Add("Second Honor");
listView1.Items.Add(nyek);
int yet = 0;
foreach (int fed in idr2)
{
if(!idr1.Contains(fed))
{
if (rcs2[yet] >= 88)
{
ListViewItem gtbs = new ListViewItem();
conek.OPEN("select concat(l_name,', ',f_name,' ',ucase(substring(m_name,1,1)),'.') from students where stud_id ='" + fed + "'");
while (conek.reader.Read())
{
gtbs.SubItems.Add(conek.reader.GetString(0));
}
conek.CLOSE();
foreach (int gfa in clsid)
{
conek.OPEN("select tmp_gr1 from class_info left join students on class_info.stud_id = students.stud_id where class_info.stud_id = '" + fed + "' and class_id = '" + gfa + "' ");
while (conek.reader.Read())
{
gtbs.SubItems.Add(Math.Round(conek.reader.GetDouble(0), 2).ToString());
}
conek.CLOSE();
}
gtbs.SubItems.Add(Math.Round(rcs2[yet], 2).ToString());
yet++;
listView1.Items.AddRange(new ListViewItem[] { gtbs });
}
}
}
第三榮譽學生
listView1.Items.Add("");
ListViewItem ne = new ListViewItem();
ne.SubItems.Add("Third Honor");
listView1.Items.Add(ne);
int yat = 0;
foreach (int fbv in idr3)
{
if (!idr2.Contains(fbv))
{
if (rcs3[yat] >= 85)
{
ListViewItem bnb = new ListViewItem();
conek.OPEN("select concat(l_name,', ',f_name,' ',ucase(substring(m_name,1,1)),'.') from students where stud_id = '" + fbv + "' ");
while (conek.reader.Read())
{
bnb.SubItems.Add(conek.reader.GetString(0));
}
conek.CLOSE();
foreach (int nmj in clsid)
{
conek.OPEN("select tmp_gr1 from class_info left join students on class_info.stud_id = students.stud_id where class_info.stud_id = '" + fbv + "' and class_id = '" + nmj + "' ");
while (conek.reader.Read())
{
bnb.SubItems.Add(Math.Round(conek.reader.GetDouble(0), 2).ToString());
}
conek.CLOSE();
}
bnb.SubItems.Add(Math.Round(rcs3[yat], 2).ToString());
yat++;
listView1.Items.AddRange(new ListViewItem[] { bnb });
}
}
}
謝謝你指出。我想盡可能地更改代碼,但我的時間不夠了。我只需要讓這個程序運行沒有錯誤。我改變了foreach循環中的值,但仍然得到相同的答案,你可以請幫我多一點 –
編輯,請嘗試。 –
我實際上是指idr1中的id列表。我用它來過濾ID。如果ird2中的id存在於idr1中,我不必添加那個。第三個榮譽 –