我正在制作一个计算学生成绩的程序,看看该学生是否是一名荣誉学生。问题是只添加了第一名荣誉学生的名单。属于第二和第三的其他人不再显示。我一直在努力解决这个问题近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 });
}
}
}
答案 0 :(得分:0)
你的问题是在foreach循环中你只迭代tmp1:
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 tmp1)
{
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 tmp1)
{
rank3.Add(gh);
}
}
无论如何,我建议你重构,因为这段代码几乎无法读取和维护。 提取类似代码的方法,使用linq的.Any()而不是.Count&gt; = 1并使用.AddRange而不是迭代并使用.Add
实际上你在更多的地方犯了同样的错误,比如第二个荣誉部分:
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 });
}
}
}
应该是:
listView2.Items.Add("");
ListViewItem nyek = new ListViewItem();
nyek.SubItems.Add("Second Honor");
listView1.Items.Add(nyek);
int yet = 0;
foreach (int fed in idr2)
{
if(!idr2.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++;
listView2.Items.AddRange(new ListViewItem[] { gtbs });
}
}
}
对于第三个人来说同样的事情......只要仔细看一下你引用第二个列表的第一个列表。