Advertisement
stronk_8s

LINQ QUERY

Sep 19th, 2024
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.04 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Student
  6. {
  7.     public int Id { get; set; }
  8.     public string Name { get; set; }
  9.     public int Grade { get; set; }
  10. }
  11.  
  12. class Program
  13. {
  14.     static void Main()
  15.     {
  16.         // Create a list of students
  17.         List<Student> students = new List<Student>
  18.         {
  19.             new Student { Id = 1, Name = "Alice", Grade = 85 },
  20.             new Student { Id = 2, Name = "Bob", Grade = 90 },
  21.             new Student { Id = 3, Name = "Charlie", Grade = 75 },
  22.             new Student { Id = 4, Name = "David", Grade = 88 }
  23.         };
  24.  
  25.         // Use LINQ query syntax to get students with grades >= 80
  26.         var highGradesQuery =
  27.             from student in students
  28.             where student.Grade >= 80
  29.             select student;
  30.  
  31.         // Execute the query and display the results
  32.         foreach (var student in highGradesQuery)
  33.         {
  34.             Console.WriteLine($"Id: {student.Id}, Name: {student.Name}, Grade: {student.Grade}");
  35.         }
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement