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