How to use LINQ select query on a list of objects in C#

1 Answer

0 votes
using System;
using System.Linq;
using System.Collections.Generic;

namespace LinqSelect
{
   class Program
   {
     static void Main(string[] args)
     {
        List<Student> studentObj = new List<Student>()
        {
          new Student() { StudentId = 1, Name = "Artemis", Marks = 81 },
          new Student() { StudentId = 2, Name = "Arthur", Marks = 92 },
          new Student() { StudentId = 3, Name = "Emmett", Marks = 78 },
          new Student() { StudentId = 4, Name = "Milo", Marks = 95 },
          new Student() { StudentId = 5, Name = "Amelia", Marks = 86 },
          new Student() { StudentId = 6, Name = "Felicity", Marks = 80 },
          new Student() { StudentId = 7, Name = "Echo", Marks = 98 }
        };
        
        var result = from s in studentObj
                     select new {SName =s.Name,SID = s.StudentId,SMarks = s.Marks };
        
        foreach (var item in result) {
           Console.WriteLine("The StudentName is {0} ID is {1} Marks is {2}", item.SName, item.SID, item.SMarks);
        }
     }
   }
   class Student
   {
      public int StudentId { get; set; }
      public string Name { get; set; }
      public int Marks { get; set; }
   }
} 


/*
run:

The StudentName is Artemis ID is 1 Marks is 81
The StudentName is Arthur ID is 2 Marks is 92
The StudentName is Emmett ID is 3 Marks is 78
The StudentName is Milo ID is 4 Marks is 95
The StudentName is Amelia ID is 5 Marks is 86
The StudentName is Felicity ID is 6 Marks is 80
The StudentName is Echo ID is 7 Marks is 98

*/

 



answered Dec 14, 2025 by avibootz
...