1

有两个列表。

我有一个学生名单,是这样制作的

List<Student> students = new List<Student>();

列表中每个学生对象的属性是 firstName、LastName 和 Fees[]。

Fees 是一个数组,其中包含列表中每个学生的一组费用。

我做了第二个这样的清单:

List<double>  totals = new List<double>();

我遍历学生名单并把每个学生的费用加起来。然后我将每个学生的总数添加到totals列表中(我的第二个列表)。

现在我需要对students列表进行排序,以便费用的最高总金额位于开头。换句话说,我需要students使用totals. 我可以totals像这样获得最高价值:

double highestTotalAmountDue = totals.Max();

如何使用此highestTotalAmountDue值对students列表进行排序,以便总费用最高的学生位于列表的开头?

澄清一下,我只需要将总费用最高的学生添加到列表顶部。其余的可以保持相同的顺序。

到目前为止,这是我的代码:

List<double> totals = new List<double>();
double tempTotal = 0;

Lis<Student> students = new Lis<Student>();

// populate the students list

foreach (var item in students)
{
    for (var i = 0; i < resultSet[0].Fees.Length; i++)
    {
        tempTotal += item.Fees[i].Amount;
    }

    totals.Add(tempTotal);
    tempTotal = 0;
}

double highestTotalAmountDue = totals.Max();

// now what to do to sort the students list by highestTotalAmountDue to put the student with the highest fee due at the top????

请帮忙。提前致谢。

4

4 回答 4

3

如果我做对了:

var orderedStudents = students
    .OrderByDescending(s => s.Fees.Sum(f => f.Amount) == highestTotalAmountDue);

这会将最大费用金额的学生放在首位,另一个将保持无序。

于 2013-10-03T22:25:57.447 回答
2

取消总数列表。您可以按如下方式跟踪最高总数,然后将学生重新插入列表顶部。

List<Student> students = new List<Student>();
// populate the students list

// Mark the student with the highest total as we find him.
Student highestTotalStudent = null;
var highestTotal = 0.0;

foreach (var student in students)
{
    var tempTotal = 0.0;

    for (var i = 0; i < resultSet[0].Fees.Length; i++)
    {
        tempTotal += student.Fees[i].Amount;
    }

    if (tempTotal > highestTotal)
    {
        // We have a new winner
        highestTotal = tempTotal;
        highestTotalStudent = student;
    }
}

// Finally, remove the located student, and re-insert her at the top of the list
students.Remove(highestTotalStudent);
students.Insert(0, highestTotalStudent);
于 2013-10-03T22:26:22.063 回答
0
double highestTotalAmountDue = totals.Max();
int highestIndex = totals.IndexOf(highestTotalAmountDue );
var student = student[highestIndex];
students.RemoveAt(highestIndex);
students.Insert(highestIndex,studet);
于 2013-10-03T22:26:27.730 回答
0

我认为关键代码是...

var studentToMove = students.FirstOrDefault(x => x.FeesTotal == students.Max(s => s.FeesTotal));
students.Remove(studentToMove);
students.Insert(0, studentToMove);



class Program
{
    static void Main(string[] args)
    {
        List<Student> students = new List<Student>()
            {
                new Student()
                    {
                        FirstName = "Joe", LastName = "Smith",
                        Fees = new Fee[]
                            {
                               new Fee()
                                   {
                                       Amount = 55
                                   }, 
                                new Fee()
                                    {
                                        Amount = 100
                                    }
                            }
                    },
                    new Student()
                    {
                        FirstName = "Jane", LastName = "Smith",
                        Fees = new Fee[]
                            {
                               new Fee()
                                   {
                                       Amount = 400
                                   }, 
                                new Fee()
                                    {
                                        Amount = 32
                                    }
                            }
                    },

                    new Student()
                    {
                        FirstName = "Sam", LastName = "Smith",
                        Fees = new Fee[]
                            {
                               new Fee()
                                   {
                                       Amount = 3
                                   }, 
                                new Fee()
                                    {
                                        Amount = 10
                                    }
                            }
                    }
            };

        var studentToMove = students.FirstOrDefault(x => x.FeesTotal == students.Max(s => s.FeesTotal));
        students.Remove(studentToMove);
        students.Insert(0, studentToMove);

        foreach (var student in students)
        {
            Console.WriteLine("Student: " + student.FirstName + " " + student.LastName);
        }
    }
}

class Student
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Fee[] Fees { get; set; }

    public decimal FeesTotal
    {
        get
        {
            if (Fees == null || Fees.Length == 0)
                return 0;

            return Fees.Sum(x => x.Amount);
        }
    }
}

class Fee
{
    public decimal Amount { get; set; }
}
于 2013-10-03T22:34:09.540 回答