[英]how to calculate the position of students in each subject in a class?
确定更清楚。 我实际上的意思是我有一个数据网格,其中包含不同的列,例如 Id 列、名字和不同主题的列,下面是示例:
when each row from the sample datagrid is selected the all the scores for each subject column is shown in the textbox1. 像这样:点击事件前的样本
并且当使用以下代码单击 button1 事件时:
foreach(int mark in txtresult.ToString())
{
if (mark < 40)
{
string[] parts = txtresults.Text.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
int[] numbers = parts.Select(p => int.Parse(p)).ToArray();
**int maximum = numbers.Max();**
int position = Array.IndexOf(numbers, maximum);
parts[position] = "1".ToString();
txtresults.Text = string.Join(" ", parts);
}
else if(mark >=40 && <50)
{
m="2th" position etc... within the above block of code
}
每个科目的分数应该根据指定范围“if(mark <40)”进行排名,但它只是排名Max(); 文本框中的值,而不是文本框中的小值。
所以我的问题是:我如何对数据网格中的每个学生进行评分就像在:列英语中,我希望能够将最高的学生排名第一,第二高的学生排名第二……等等,以及循环遇到类似分数的地方文本框将有类似的 position 并继续排名。 直到每个学生的所有分数都被排名; (例如,Eng =56, 78, 89, 76, 89.. 从这个 89 => 1st; 89 =>1st; 78 => 3rd; 76 =>4th position; & 56=> 5th Z4757FE07FD492A8BE0EA6A760D683)
您可以使用 LINQ 对集合的项目进行分组。
以下示例创建三个组:
使用值规则的“低”、“中”、“高”
n < 10
, n >= 10 && n < 100
, n >= 100
:
var numericString = "1 2 12 15 123 757";
var numberGroups = numericString
.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.Select(numericString => int.Parse(numericString))
.GroupBy(number => number < 10
? "Low"
: number < 100
? "Mid"
: "High");
// Access the groups
foreach (IGrouping<string, int> numberGroup in numberGroups)
{
string groupName = numberGroup.Key; // E.g. "High"
IEnumerable<int> groupValues = numberGroup; // 123, 757
}
并将相等的值分组:
var numericString = "100 20 12 100 20 20 48 100";
var numberGroups = numericString
.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
.Select(numericString => int.Parse(numericString))
.GroupBy(number => number);
// Access the groups
foreach (IGrouping<int, int> numberGroup in numberGroups)
{
int groupName = numberGroup.Key; // E.g. 100
List<int> groupValues = numberGroup.ToList(); // 100, 100, 100
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.