3

まず、私は2つのクラスを持っています

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

10 個の学生オブジェクトがあるとします Student[] スタッド = 新しい Student[10]

スタッド[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"

スタッド [1] には 2 つの手数料 ( Fee[2] ) があり、それらは

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

そしてスタッド[2]は......

学生の同じ順序を維持しながら、FeeDueDateの昇順で手数料である2番目のコレクションをソートする必要があります

ソート後は

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()
        }

EDITリストがnullでない ことを確認するには、次の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

さて、これはあなたの2番目の問題を解決するでしょう。それを理解するために一生懸命努力してください...またはあなたの先生は答えがあなた自身のものではないことを知っているでしょう...

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 に答える
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 に答える