在另一个对象集合中对对象集合进行排序

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]有两个费用(Fee[2])时,它们是:

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

我可以帮您进行翻译。以下是需要翻译的内容:

并且stud[2]有......

我需要对第二个集合(即Fee)按照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

每个学生都有一组“费用”吗?另外,“你尝试过什么?” - Ryan Gates
这完全取决于你在第一个类中如何实现你的字段费用。它是一个列表吗?还是一个数据库对象? - user287107
我尝试过以下代码:objStudentWithFeeData = objStudentWithFeeData.OrderBy(stu => stu.Fees.DueDate).ToList(); 但似乎在stu.Fees中找不到DueDate。Student/Fee类是在WCF服务端实现的,我无法访问它。 - user1618180
1
实际上,无论多么诱人,你都不应该通过在StackOverflow上询问来完成你的学校作业... - Ingo
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;}
    }

感谢您的时间,我尝试了您的方法,但是在 student.Fees.OrderByDescending(c => c.DueDate).ToList(); 中出现了错误。无法隐式转换类型“System.Collections.Generic.List<StudentList.RServiceReference.Fee>”为“StudentList.RServiceReference.Fee[]”。 - user1618180
@user1618180 我已经更新了答案 - 应该不是 OrderBy Desending,而是 OrderBy。检查一下学生的 Fees 列表是否为空。你可以在 Stundent 构造函数中初始化它。请查看编辑。 - Jens Kloster
抱歉,仍然给我错误。无法隐式转换类型“System.Collections.Generic.List<StudentList.RServiceReference.Fee>”为“StudentList.RServiceReference.Fee[]”。 - user1618180
1
谢谢Jens!!!在这个学校作业上遇到了麻烦。已经花了2个小时了。 - user1618180
假设在费用中有另一个属性FeeAmount,现在我需要按学生(按收集顺序)再次排序,以获取最高的FeeAmount金额。我的电子邮件是d87c@live.ca,也许我们可以在那里交流。 - user1618180
显示剩余5条评论

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

这是我的整个控制台应用程序的Program.cs的副本。我总是太慢了 :-) 无论如何,祝你玩得愉快。 - Ingo
谢谢Ingo!stackoverflow上有很多知识渊博的人。 - user1618180
假设在费用中有另一个属性FeeAmount,现在我需要按照学生的FeeAmount最高值再次排序。我的电子邮件是d87c@live.ca,也许我们可以在那里交流。 - user1618180
只需将“orderby fee.FeeDueDate ascending”替换为“orderby fee.FeeAmount ascending”,如果您想创建一个方法,可以在其中传递属性名称,以便进行排序,我可能会像Jens一样使用linq表达式,并将属性作为Lambda传递给该方法,因此在方法内部,您可以将Lambda放置到linq表达式中的“.OrderBy(Lambda)。”..哦,然后您需要将金额字段的总和聚合到学生中。 - Ingo

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接