Create list by using 'Student' class but from the another list with LINQ C#

Viewed 46

Here is the problem I am trying to solve:

Using the Student class, create a list of a few students from the people list using LINQ.

var people = new List<Person>
{
    new Person("John", "Doe", 20),
    new Person("Alex", "Jhones", 18)
};


var students = new List<Student>();

students = people.Select(p => p).ToList();

foreach (var i in students)
{
    Console.WriteLine();
}

I tried to write the line students=people.Select(p=>p).ToList(); However, parameter p refers to the Person class, I need to write something else there so the parameter refers to Student class.

public sealed class Student
{
    public uint ID { get; }
    public string Name { get; }
    public string Surname { get; }
    public uint Grade { get; }

    public Student(uint id, 
                   string name, 
                   string surname, 
                   uint grade)
    {
        ID = id;
        Name = name;
        Surname = surname;
        Grade = grade;
    }

    public override string ToString() => $"Student {ID} named {Name} {Surname} got {Grade}.";
}

public sealed class Person
{
    public string Name { get; set; }
    public string Surname { get; }
    public uint Age { get; }

    public Person(string name, 
                  string surname, 
                  uint age)
    {
        Name = name;
        Surname = surname;
        Age = age;
    }
}
0 Answers
Related