question

PolachanPaily-6650 avatar image
0 Votes"
PolachanPaily-6650 asked Viorel-1 commented

Get the value from Model into List object

I have a student class with the following attributes and values. I am storing the first record of a student in a class into student object variable and how could I get the value of the first student in to List. How can I get the value of first student from the class into firstStudent variable

 List<Student> studentList = new List<Student>() { 
             new Student() { StudentID = 1, StudentName = "John"} ,
             new Student() { StudentID = 2, StudentName = "Moin"} ,
             new Student() { StudentID = 3, StudentName = "Bill"} ,
             new Student() { StudentID = 4, StudentName = "Ram"} ,
             new Student() { StudentID = 5, StudentName = "Ron"} 
         };
    
 Student student = new Student();
    
  foreach (var students in class.students)
    {
           
        student = students;
        break;
            
    }
    
 List<Student> firstStudent = new List<Student>();  

fistStudent should be stored with value of a first student from the class


dotnet-aspnet-core-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.


You already have the first student, so use firstStudent.Add(student) to add it. Or use this: firstStudent = studentList.Take(1).ToList( ).


0 Votes 0 ·

1 Answer

ZhiLv-MSFT avatar image
1 Vote"
ZhiLv-MSFT answered

Hi @PolachanPaily-6650,

You can try to use the Enumerable.First() Method or LINQ Query to filter the data and get the first student.

Code like this:

          List<Student> studentList = new List<Student>() {
              new Student() { StudentID = 1, StudentName = "John"} ,
              new Student() { StudentID = 2, StudentName = "Moin"} ,
              new Student() { StudentID = 3, StudentName = "Bill"} ,
              new Student() { StudentID = 4, StudentName = "Ram"} ,
              new Student() { StudentID = 5, StudentName = "Ron"}
          };
         //use the first method.
         Student firststudent = studentList.First();
         //use the LINQ query 
         Student firststudent2 = studentList.Where(c => c.StudentID == 1).First();

         //find the student by id.
         List<Student> filterstudent = studentList.Where(c => c.StudentID == 1).ToList(); 

The result as below:

205218-2.gif


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Best regards,
Dillion


2.gif (288.1 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.