LINQ: Handling Of Array, List Object And GROUP BY

Language Integrated Query: LINQ

LINQ introduced in .NET 3.5, mainly used for Query IN Memory and other source of collection and objects.

LINQ having the following different flavour:

  1. LINQ To SQL
  2. LINQ To XML
  3. LINQ To DataSet
  4. LINQ To Entities
  5. LINQ To Object

With help of LINQ, traditional 6 lines comes down to 1 line, LINQ very. Basically, LINQ is a collection of extensions methods and declared in static classes.

Advantages

  1. Reduces the total lines of code.
  2. Writing the code very fast and with improved IntelliSense.
  3. Compile time type checking of objects.
  4. Very fast transformation of One DataType to Another DataType.

We can write LINQ query on the following basis:

  1. Query Syntax : Query type style.
  2. Method (Lambda) Syntax : Query in Lambda style.

Through the view of LINQ code, you will come to know the benefits of LINQ.

In this article, I have explained how to handle data of Array and List objects.

To do the practice of the following code, please create console application.

_friends is collection of array records, where I filtered PLACE=MUMBAI records.

Example

  1. Friend[] MumbaiFriends = (_friends.Where(x => x.Place == "Mumbai").ToArray());  
_friends is collection of array records, where I GroupBy on PLACE records.

Example:
  1. var PlaceGroupData = (_friends.GroupBy(s => s.Place).ToArray());  
  2. _friendsList is collection of LIST records, where I filtered PLACE=JODHPUR records.  
  3. var JodhpurFriends = (_friendsList.Where(x => x.Place == "Jodhpur").ToList<Friend>());  
CONSOLE APPLICATION CODE

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace LinqIntro  
  8. {  
  9.     class Program   
  10.     {  
  11.         static void Main(string[] args)  
  12.       {  
  13.             Friend[] _friends =   
  14.               {  
  15.                 new Friend  
  16.                 {  
  17.                     FriendID = 1, FriendName = "Rajesh", MobileNumber = "982111111", Place = "Mumbai", Technology = ".NET"  
  18.                 },  
  19.                 new Friend   
  20.                 {  
  21.                     FriendID = 2, FriendName = "Mahesh", MobileNumber = "982122222", Place = "Mumbai", Technology = ".NET"  
  22.                 },  
  23.                 new Friend  
  24.                 {  
  25.                     FriendID = 3, FriendName = "Suresh", MobileNumber = "982133333", Place = "Mumbai", Technology = "JAVA"  
  26.                 },  
  27.                 new Friend  
  28.                 {  
  29.                     FriendID = 4, FriendName = "Jayesh", MobileNumber = "982144444", Place = "Mumbai", Technology = ".NET"  
  30.                 },  
  31.                 new Friend  
  32.                 {  
  33.                     FriendID = 5, FriendName = "Pritesh", MobileNumber = "98115555", Place = "Jodhpur", Technology = ".NET"  
  34.                 },  
  35.                 new Friend   
  36.                 {  
  37.                     FriendID = 6, FriendName = "Mayuresh", MobileNumber = "98116666", Place = "Jodhpur", Technology = "JAVA"  
  38.                 },  
  39.                 new Friend   
  40.                 {  
  41.                     FriendID = 7, FriendName = "Tarunesh", MobileNumber = "981177777", Place = "Jodhpur", Technology = ".NET"  
  42.                 },  
  43.                 new Friend   
  44.                 {  
  45.                     FriendID = 8, FriendName = "Ramesh", MobileNumber = "981188888", Place = "Phalodi", Technology = "JAVA"  
  46.                 },  
  47.                 new Friend   
  48.                 {  
  49.                     FriendID = 9, FriendName = "Kamlesh", MobileNumber = "981199999", Place = "Phalodi", Technology = ".NET"  
  50.                 }  
  51.             };  
  52.   
  53.             // Filter the RecordSet  
  54.             Friend[] MumbaiFriends = (_friends.Where(x => x.Place == "Mumbai").ToArray());  
  55.   
  56.             Console.WriteLine("Aaray Working Example");  
  57.             Console.WriteLine("=====================");  
  58.             Console.WriteLine("Printing Mumbai Friends.");  
  59.             foreach(Friend frnd in MumbaiFriends)  
  60.             {  
  61.                 Console.WriteLine(frnd.FriendID.ToString() + " " + frnd.FriendName + " " + frnd.Place + " " + frnd.Technology);  
  62.             }  
  63.             Console.ReadKey();  
  64.             Console.WriteLine();  
  65.             Console.WriteLine();  
  66.             Console.WriteLine();  
  67.             Console.WriteLine("Printing DOT NET Technology Friends.");  
  68.   
  69.             Friend[] DotNetFriends = (_friends.Where(x => x.Technology == ".NET").ToArray());  
  70.   
  71.             foreach(Friend frnd in DotNetFriends) {  
  72.                 Console.WriteLine(frnd.FriendID.ToString() + " " + frnd.FriendName + " " + frnd.Place + " " + frnd.Technology);  
  73.             }  
  74.             Console.ReadKey();  
  75.             Console.WriteLine();  
  76.             Console.WriteLine();  
  77.             Console.WriteLine();  
  78.             // Place Group By  
  79.             var PlaceGroupData = (_friends.GroupBy(s => s.Place).ToArray());  
  80.             Console.WriteLine("Place ----- Nos. of Friends");  
  81.             foreach(var Place in PlaceGroupData)  
  82.             {  
  83.                 Console.WriteLine(Place.Key.ToString() + " ----- " + Place.Count().ToString());  
  84.             }  
  85.             Console.ReadKey();  
  86.             Console.WriteLine();  
  87.             Console.WriteLine();  
  88.             Console.WriteLine();  
  89.             Console.WriteLine();  
  90.   
  91.             //  
  92.             Console.WriteLine("List Working Example");  
  93.             Console.WriteLine("=====================");  
  94.             List < Friend > _friendsList = new List < Friend > ()  
  95.             {  
  96.                 new Friend  
  97.                 {  
  98.                     FriendID = 1, FriendName = "Rajesh", MobileNumber = "982111111", Place = "Mumbai", Technology = ".NET"  
  99.                 },  
  100.                 new Friend   
  101.                 {  
  102.                     FriendID = 2, FriendName = "Mahesh", MobileNumber = "982122222", Place = "Mumbai", Technology = ".NET"  
  103.                 },  
  104.                 new Friend  
  105.                 {  
  106.                     FriendID = 3, FriendName = "Suresh", MobileNumber = "982133333", Place = "Mumbai", Technology = "JAVA"  
  107.                 },  
  108.                 new Friend  
  109.                 {  
  110.                     FriendID = 4, FriendName = "Jayesh", MobileNumber = "982144444", Place = "Mumbai", Technology = ".NET"  
  111.                 },  
  112.                 new Friend  
  113.                 {  
  114.                     FriendID = 5, FriendName = "Pritesh", MobileNumber = "98115555", Place = "Jodhpur", Technology = ".NET"  
  115.                 },  
  116.                 new Friend  
  117.                 {  
  118.                     FriendID = 6, FriendName = "Mayuresh", MobileNumber = "98116666", Place = "Jodhpur", Technology = "JAVA"  
  119.                 },  
  120.                 new Friend   
  121.                 {  
  122.                     FriendID = 7, FriendName = "Tarunesh", MobileNumber = "981177777", Place = "Jodhpur", Technology = ".NET"  
  123.                 },  
  124.                 new Friend  
  125.                 {  
  126.                     FriendID = 8, FriendName = "Ramesh", MobileNumber = "981188888", Place = "Phalodi", Technology = "JAVA"  
  127.                 },  
  128.                 new Friend  
  129.                 {  
  130.                     FriendID = 9, FriendName = "Kamlesh", MobileNumber = "981199999", Place = "Phalodi", Technology = ".NET"  
  131.                 }  
  132.             };  
  133.   
  134.             // Filter the RecordSet  
  135.             var JodhpurFriends = (_friendsList.Where(x => x.Place == "Jodhpur").ToList < Friend > ());  
  136.             Console.WriteLine("Printing Jodhpur Friends.");  
  137.             foreach(Friend frnd in JodhpurFriends)   
  138.             {  
  139.                 Console.WriteLine(frnd.FriendID.ToString() + " " + frnd.FriendName + " " + frnd.Place + " " + frnd.Technology);  
  140.             }  
  141.             Console.ReadKey();  
  142.             Console.WriteLine();  
  143.             Console.WriteLine();  
  144.             Console.WriteLine();  
  145.             Console.WriteLine("Printing DOT NET Technology Friends.");  
  146.   
  147.             var JavaFriends = (_friends.Where(x => x.Technology == "JAVA").ToList < Friend > ());  
  148.   
  149.             foreach(Friend frnd in JavaFriends)  
  150.             {  
  151.                 Console.WriteLine(frnd.FriendID.ToString() + " " + frnd.FriendName + " " + frnd.Place + " " + frnd.Technology);  
  152.             }  
  153.             Console.ReadKey();  
  154.             Console.WriteLine();  
  155.   
  156.         }  
  157.     }  
  158.   
  159.     public class Friend  
  160.     {  
  161.         public int FriendID  
  162.       {  
  163.             get;  
  164.             set;  
  165.         }  
  166.         public string FriendName  
  167.         {  
  168.             get;  
  169.             set;  
  170.         }  
  171.         public string MobileNumber  
  172.         {  
  173.             get;  
  174.             set;  
  175.         }  
  176.         public string Place  
  177.         {  
  178.             get;  
  179.             set;  
  180.         }  
  181.         public string Technology  
  182.         {  
  183.             get;  
  184.             set;  
  185.         }  
  186.     }  
  187.   
  188. }  


Similar Articles