Home > DeveloperSection > Blogs > I LINQ to Array.

I LINQ to Array.

4 Comment(s)
 4851  View(s)
Rate this:


In this blog I will give you a small demonstration on LINQ to ARRAY. Here I will explain you that how to use LINQ for array to perform certain task such as sorting of array and searching records from array. Here I had created two methods in Program class named displayValueInOrder() which accept two parameters, one string type array which needs to be sorted and other one is string value which is either ASC or DESC. Second method is searchSingleRecord() which search a record from array and also accept two parameters one is string array on which searching needs to be perform and second one is searchVale which needs to be search.

Here is simple program which clear your concept about LINQ to array.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LinqDemo
    class Program
        /// <summary>
        /// This method display string array records either in ascending order
        /// or descending order on basis of order parameter. For performing
        /// this task I have use LINQ. We use orderby clause to specify ordering
        /// followed by variable name then order which is either ascending or
        /// descending. If you do not specify order then linq generate list
        /// in ascending order.
        /// </summary>
        public void displayValueInOrder(string[] records, string order)
            if (order.Equals("ASC"))
                var record = from std in records
                       orderby std ascending
                       select std;

                //Displaying record in ascending order.
                foreach (string data in record)
                var record = from std in records
                       orderby std descending
                       select std;

                //Displaying record in descending order.
                foreach (string data in record)

        /// <summary>
        /// This method search record from array and display first search value.
        /// searchSingleRecord() method takes two parameter first records array
        /// in which search operation needs to be perform and second one is
        /// search value which needs to be searched. After searching record
        /// it display first value if exists from search record.
        /// </summary>
        public void searchSingleRecord(string[] records,string searchValue)
            string record = (from std in records
                             where std.Equals(searchValue)
                             select std).FirstOrDefault();


        static void Main(string[] args)
            string[] studentName = { "Awadhendra", "Ankit", "James", "John", "Cat", "Shilpa", "Shivam", "Haider", "Ashish" };

            Program pr = new Program();
            pr.displayValueInOrder(studentName, "ASC");

            pr.searchSingleRecord(studentName, "Avanish");


Thanks for reading this blog. Please provide any suggestion and feedback on this blog and suggest me some other topic in which I need to write articles or blogs.

how use datagridview control pop-up menu functions

By aken H on   6 years ago

Hi  Awadhendra Tiwari ,

 Please provide about  how use datagridview control pop-up menu functions to  right key.

Thanks in advance.

datagridview control pop-up menu functions

By Jenry Hock on   5 years ago

Hi Aken,

Could you please explain your problem in more detail. What do you want to do with datagridview in this context.


Linq to Array.

By James Smith on   5 years ago
Thanks Awadhendra for this useful example.

Linq to array.

By Rohit Kesharwani on   5 years ago

Wow! Its really solve my problem............


Thanks Awadhendra.

Don't want to miss updates? Please click the below button!

Follow MindStick