首先我有2个班级
class Student
{
int StudentID
string Name
Fee fee
}
class Fees
{
int FeeID
string FeeName
DateTime FeeDueDate
}
假设有10个学生对象学生[] stud = new Student [10]
如果stud [0]有4个费用(费用[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笔费用(费用[2]),但他们是
FeeID=2, FeeName="Books", FeeDueDate="2014/06/20"
FeeID=4, FeeName="School Dorm", FeeDueDate="2013/03/26"
和stud [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
答案 0 :(得分: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
列表不为null,您可以像这样初始化它:
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;}
}
答案 1 :(得分: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);
}
}
}
答案 2 :(得分: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);
}
}
}