3

首先我有2节课

class Student
{
    int StudentID
    string Name
    Fee fee
}
class Fees
{
    int FeeID
    string FeeName
    DateTime FeeDueDate
}

假设有 10 个学生对象 Student[] stud = new Student[10]

如果 stud[0] 有 4 笔费用( Fee[4] ),他们是

FeeID=1, FeeName="Books", FeeDueDate="2014/06/25"
FeeID=2, FeeName="Tuition", FeeDueDate="2013/03/21"
FeeID=3, FeeName="Sports Equipment", FeeDueDate="2013/03/18"
FeeID=4, FeeName="School Dorm", FeeDueDate="2013/07/26"

而 stud[1] 有 2 项费用( Fee[2] ),它们是

FeeID=2, FeeName="Books", FeeDueDate="2014/06/20"
FeeID=4, FeeName="School Dorm", FeeDueDate="2013/03/26"

和螺柱 [2] 有......

我需要对第二个集合进行排序,即按 FeeDueDate 递增的费用,同时保持学生的相同顺序

所以排序后会是

stud[0] =>

FeeID=3, FeeName="Sports Equipment", FeeDueDate="2013/03/18"
FeeID=2, FeeName="Tuition", FeeDueDate="2013/03/21"
FeeID=4, FeeName="School Dorm", FeeDueDate="2013/07/26"
FeeID=1, FeeName="Books", FeeDueDate="2014/06/25"

stud[1] =>

FeeID=4, FeeName="School Dorm", FeeDueDate="2013/03/26"
FeeID=2, FeeName="Books", FeeDueDate="2014/06/20"

如何才能做到这一点?谢谢

此图像还显示了我的对象集合 http://oi50.tinypic.com/wuq9o3.jpg

4

3 回答 3

2

假设您的课程如下所示:

   public class Student
    {
        public int StudentID { get; set;}
        public string Name { get; set;}
        public List<Fee> Fees {get;set;}
    }

    public class Fee
    {
        public int FeeID { get; set;}
        public string FeeName { get; set;}
        public DateTime FeeDueDate { get; set; }
    }

你可以这样做:

        Student[] studs = new Student[10]

        //somehow fill the array

        foreach (var student in studs)
        {
            student.Fees = student.Fees.OrderBy(c => c.FeeDueDate).ToList()
        }

编辑 要确保Fees列表不为空,您可以像这样初始化它:

   public class Student
   {
       public Student()
       {
         Fees = new List<Fees>();
       }

        public int StudentID { get; set;}
        public string Name { get; set;}
        public List<Fee> Fees {get;set;}
    }
于 2013-03-12T21:39:08.237 回答
1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var students = new[] {
                new Student()   
                    { StudentID = 0, Name = "Mark", fees =
                        new List<Fee>() {
                            new Fee() { FeeID=1, FeeName="Books", FeeDueDate=new DateTime(2014,06,25) },
                            new Fee() { FeeID=2, FeeName="Tuition", FeeDueDate=new DateTime(2013,03,21) },
                            new Fee() { FeeID=3, FeeName="Sports Equipment", FeeDueDate=new DateTime(2013,03,18) },
                            new Fee() { FeeID=4, FeeName="School Dorm", FeeDueDate=new DateTime(2013,07,26)}
                        }
                    },
                new Student()
                    { StudentID = 1, Name = "Tom", fees =
                        new List<Fee>() {
                            new Fee() { FeeID=2, FeeName="Books", FeeDueDate=new DateTime(2014,06,20) },
                            new Fee() { FeeID=4, FeeName="School Dorm", FeeDueDate=new DateTime(2013,03,26) }
                        }
                    }
                };

            var sorted = from student in students
                         orderby student.StudentID
                         select new
                         {
                             stud = student.StudentID,
                             fees =
                                 from fee in student.fees
                                 orderby fee.FeeDueDate ascending
                                 select fee
                         };

            foreach (var item in sorted)
            {
                Console.WriteLine("stud[{0}] => ", item.stud);
                foreach (var f in item.fees)
                {
                    Console.WriteLine(f.ToString());
                }
                Console.WriteLine();
            }
        }
    }


    public class Student
    {
        public int StudentID {get; set;}
        public string Name {get; set;}
        public List<Fee> fees {get; set;}
    }

    public class Fee
    {
        public int FeeID {get; set;}
        public string FeeName {get; set;}
        public DateTime FeeDueDate { get; set; }

        public override string ToString()
        {
             return String.Format("FeeID={0,2}, FeeName={1,20}, FeeDueDate={2}", FeeID, FeeName, FeeDueDate);
        }
    }
}
于 2013-03-12T21:53:39.643 回答
1

好吧,这将解决您的第二个问题。努力去理解它……否则你的老师会知道答案不是你自己的……

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var students = new[] {
                new Student()   
                    { StudentID = 0, Name = "Mark", fees =
                        new List<Fee>() {
                            new Fee() { FeeID=1, FeeName="Books", FeeDueDate=new DateTime(2014,06,25), FeeAmount = 1.30 },
                            new Fee() { FeeID=2, FeeName="Tuition", FeeDueDate=new DateTime(2013,03,21), FeeAmount = .30 },
                            new Fee() { FeeID=3, FeeName="Sports Equipment", FeeDueDate=new DateTime(2013,03,18), FeeAmount = .80 },
                            new Fee() { FeeID=4, FeeName="School Dorm", FeeDueDate=new DateTime(2013,07,26), FeeAmount = 0}
                        }
                    },
                new Student()
                    { StudentID = 1, Name = "Tom", fees =
                        new List<Fee>() {
                            new Fee() { FeeID=2, FeeName="Books", FeeDueDate=new DateTime(2014,06,20), FeeAmount = 1.50 },
                            new Fee() { FeeID=4, FeeName="School Dorm", FeeDueDate=new DateTime(2013,03,26), FeeAmount = 4.00 }
                        }
                    }
                };

            var sorted = (from student in students
                         select new
                         {
                             stud = student.StudentID,
                             name = student.Name,
                             feesum = student.fees.Sum(x => x.FeeAmount),
                             fees =
                                 from fee in student.fees
                                 orderby fee.FeeDueDate ascending
                                 select fee
                         }).OrderByDescending(s => s.feesum);

            foreach (var item in sorted)
            {
                Console.WriteLine("stud[{0}] => {1}, Fees: {2}", item.stud, item.name, item.feesum);
                foreach (var f in item.fees)
                {
                    Console.WriteLine(f.ToString());
                }
                Console.WriteLine();
            }
        }
    }


    public class Student
    {
        public int StudentID {get; set;}
        public string Name {get; set;}
        public List<Fee> fees {get; set;}
    }

    public class Fee
    {
        public int FeeID {get; set;}
        public string FeeName {get; set;}
        public DateTime FeeDueDate { get; set; }
        public double FeeAmount { get; set; }

        public override string ToString()
        {
             return String.Format("FeeID={0,2}, FeeName={1,20}, FeeDueDate={2}", FeeID, FeeName, FeeDueDate);
        }
    }
}
于 2013-03-12T22:20:35.213 回答