Home > Software engineering >  Get the value from Model into List object
Get the value from Model into List object

Time:05-25

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

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

How can I get the value of student from the class into firstStudent

CodePudding user response:

Using list, you can get first object with .FirstOrDefault

E.g.,

studentList.FirstOrDefault()

If you're using LINQ, you can get this with conditions, like filtering IDs with a Lambda expression; e.g.,

studentList.FirstOrDefault(e => e.StudentID == <value>) //replace <value> with appropriate value.

Your code will be 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"} 
        };

 Student student = new Student();

 foreach (var students in class.students)
   {
       
       student = students
       break
        
   }

List<Student> firstStudent = studentList.FirstOrDefault();

CodePudding user response:

maybe something like this. use linq take to just get the first one from the list

here is the fiddle https://dotnetfiddle.net/7eU8eZ

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"} 
        };
        
        Class myClass = new Class(){
            ClassId = 1,
            Students = studentList
        };
        
        List<Student> firstStudent = 
        myClass.Students.OrderBy(x => x.StudentID).Take(1).ToList();
    };

CodePudding user response:

you can use index with list, it will create a list with one student

List<Student> firstStudent = new List<Student> { studentList[0] };

if you want just a first student

Student firstStudent = studentList[0] ;
  • Related