C# Program to Display the Smallest Numbers in an Array using FROM Clause LINQ

This is a C# Program to display the smallest numbers in an array using from clause linq.

Problem Description

This C# Program Displays the Smallest numbers in an Array using FROM Clause LINQ.

Problem Solution

Here the from clause allows to obtain the result of an expression inside the query expression.

Program/Source Code

Here is source code of the C# Program to Display the Smallest numbers in an Array using FROM Clause LINQ. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 *  C# Program to Display the Smallest numbers in an Array using FROM Clause LINQ
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication2
{
    class program
    {
        static void Main()
        {
            int[] numbers = { 50,30,45,10,60,100,500,300,40,22,44,55,66,1000 };
            var program = from num in numbers
                          where num < 50
                          select num;
            Console.WriteLine("Numbers less than 50 are :");
            foreach (int i in program)
            {
                Console.Write(i + " ");
            }
            Console.ReadLine();
        }
    }
}
Program Explanation

This C# program is used to print the smallest numbers in an array using from clause LINQ. We have already defined the values of ‘array[]’ variable. Here ‘from’ clause allows obtaining the result of an expression inside the query expression.

advertisement
advertisement

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 the values of an element less than 50.

Runtime Test Cases
 
Numbers less than 50 are :
30 45 10 40 22 44

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
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.