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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Check this: MCA MCQs | C# Books
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.