Filter List between two dates and display its data

Hi. I am a newbie in Unity. How can I filter list between two dates. I have a class student below. I want to display students enrolled 20 days ago until now using enrolledDate.

class student
{
string studentName;
string address;
DateTime enrolledDate;
}

Example student

List<student> studentList = new List<student>();

Assuming students below are added to list.
John Doe
Chicago
03/01/2017

Michael
Chicago
03/01/2017

Mary Jane
Illinois
2/05/2017

the output should be:
John Doe
Michael

I really appreciate your help. Thanks

using System.Linq;

public List<student> GetStudents(int days,List<student> allStudents)
{
        return allStudents.Where(x=> ( DateTime.Now-x.enrolledDate).TotalDays >= days).ToList();
}
1 Like

Hi sir thank you for your answer.I just want to know how can I use or call your code? :slight_smile:

Just wherever you have a List made.

// Some class that contains your student list
List<student> allStudents = new List<student>();

// Some other class would call this script with the
// the number of days
public void DisplayStudents(int enrolledDays)
{
         List<student> students = GetStudents(enrolledDays,allstudents);
         foreach(student oneStudens in students)
         {
                //Code here to print out the student data
                // Or write it to a file
         }
}

Hard to say exactly without knowing your code, or what your going to do with the list of students that meet the requirements

1 Like