Home / DeveloperSection / Blogs / I LINQ to Array.

I LINQ to Array.

Anonymous User 7174 11-Aug-2011


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)                     Console.WriteLine(data);             }             else             {                 var record = from std in records                        orderby std descending                        select std;
                //Displaying record in descending order.                 foreach (string data in record)                     Console.WriteLine(data);             }         }
        /// <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();
            Console.WriteLine(record);         }
        static void Main(string[] args)         {             string[] studentName = { "Awadhendra", "Ankit", "James", "John", "Cat", "Shilpa", "Shivam", "Haider", "Ashish" };
            Program pr = new Program();             pr.displayValueInOrder(studentName, "ASC");
            Console.WriteLine();             pr.searchSingleRecord(studentName, "Avanish");
            Console.ReadKey();                     }     } }

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.

Updated 18-Sep-2014
I am a content writter !

Leave Comment

Please enter subject.
Please enter comments message.


Liked By