3

First I have 2 classes

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

let say there are 10 students objects Student[] stud = new Student[10]

and if stud[0] has 4 fees ( Fee[4] ) and they are

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"

while stud[1] has 2 fees ( Fee[2] ) and they are

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

and stud[2] has ......

I need the sort the second collection which is the Fee by FeeDueDate asscending while Keep the same order of student

so after sorting would be

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"

How can this be done? Thanks

Also this image shows my collections of objects http://oi50.tinypic.com/wuq9o3.jpg

4
  • Does each student have a collection of Fees's? Also What have you tried? Commented Mar 12, 2013 at 21:32
  • that completely depends, how your field fee is implemented in your first class. it is a list, is it a database object? Commented Mar 12, 2013 at 21:33
  • i tried: objStudentWithFeeData = objStudentWithFeeData.OrderBy(stu => stu.Fees.DueDate).ToList(); but it seems it cant find the DueDate in stu.Fees The Student/Fee class is implemented on the WCF service end. I have no access to it Commented Mar 12, 2013 at 21:47
  • 1
    actually you shouldnt get your schoolwork done by asking on stackoverflow no matter how tempting ... Commented Mar 12, 2013 at 21:59

3 Answers 3

2

Assuming that you classes look like this:

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

You could do it like this:

        Student[] studs = new Student[10]

        //somehow fill the array

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

EDIT To make sure the the Fees list is not null, you could initialize it like this:

   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;}
    }
Sign up to request clarification or add additional context in comments.

10 Comments

Thanks for your time, I tried your way but there is an error with student.Fees.OrderByDescending(c => c.DueDate).ToList(); Cannot implicitly convert type 'System.Collections.Generic.List<StudentList.RServiceReference.Fee>' to 'StudentList.RServiceReference.Fee[]
@user1618180 I have updated the answer - it should not be OrderBy Desending, just OrderBy. Check that a studens list of Fees is not null. you cound init. it in the Stundent constructor. See edit
Lol, always nice to alt-tab back and see the EXACT SAME solution :). +1 nonetheless :)
Sorry, still given me error Cannot implicitly convert type 'System.Collections.Generic.List<StudentList.RServiceReference.Fee>' to 'StudentList.RServiceReference.Fee[]'
thanks Jens!!!! Had trouble with this school assignment. Already spent 2 hours on it
|
1

Well, this would solve your 2nd Problem. Try hard to understand it... or your teacher will know the answer is not your own...

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

Comments

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

4 Comments

well its a copy of my whole Program.cs of a console app. As always I seem to be too slow :-) have fun anyway
Thanks Ingo!, stackoverflow is full of awesome knowledgable people
let say in the Fee has another property, FeeAmount now I need to sort again by student with highest amount of FeeAmount my email is [email protected] maybe we can have conversation there
just replace orderby fee.FeeDueDate ascending by orderby fee.FeeAmount ascending ... if you want to create a method where you can pass the property Name after which the sorting shall be done, I would probably use a linq Expression like Jens did, and pass the property as a Lambda to the method so inside the method you can place the Lambda as you got it into the linq expressions .OrderBy( Lambda ). .. oh and then you would Need to Aggregate the sum of the amount field to the student

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.