C# Program to Implement Let Condition using LINQ
Posted by Superadmin on August 14 2022 14:14:14

C# Program to Implement Let Condition using LINQ

 

 

This is a C# Program to implement let condition using linq.

Problem Description

This C# Program Implements Let Condition using LINQ.

Problem Solution

Here the Let clause allows to store the result of an expression inside the query expression.

Program/Source Code

Here is source code of the C# Program to Implement Let Condition using LINQ. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Implement Let Condition using LINQ
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
    class Student
    {
        public string Name { get; set; }
        public string Regno { get; set; }
        public int Marks { get; set; }
 
    }
    class Program
    {
        static void Main(string[] args)
        {
            //Object Initialization for Student class
            List<Student> objStudent = new List<Student>{
                    new Student{ Name="Tom",Regno="R001",Marks=80},
                    new Student{ Name="Bob",Regno="R002",Marks=40},
                    new Student{ Name="jerry",Regno="R003",Marks=25},
                    new Student{ Name="Syed",Regno="R004",Marks=30},
                    new Student{ Name="Mob",Regno="R005",Marks=70},
                };
 
            var objresult = from stu in objStudent
                            let totalMarks = objStudent.Sum(mark => mark.Marks)
                            let avgMarks = totalMarks / 5
                            where avgMarks > stu.Marks
                            select stu;
            foreach (var stu in objresult)
            {
                Console.WriteLine("Student: {0} {1}", stu.Name, stu.Regno);
 
            }
            Console.ReadLine();
        }
    }
Program Explanation

This C# program is used to implement let condition using LINQ. Create a student class with Name, Regno, Marks variables. The program class is used for object initialization for student class. The let clause allows storing the result of an expression inside the query expression.

 

The where clause is used in a query expression to specify which elements from the data source will be returned in the query expression. The foreach() function is used to print only the average marks greater than student marks.

Runtime Test Cases
 
Student: Bob R002
Student: jerry R003
Student: Syed R004