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:
- LINQ To SQL
- LINQ To XML
- LINQ To DataSet
- LINQ To Entities
- 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
- Reduces the total lines of code.
- Writing the code very fast and with improved IntelliSense.
- Compile time type checking of objects.
- Very fast transformation of One DataType to Another DataType.
We can write LINQ query on the following basis:
- Query Syntax : Query type style.
- 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
- Friend[] MumbaiFriends = (_friends.Where(x => x.Place == "Mumbai").ToArray());
_friends is collection of array records, where I GroupBy on PLACE records.
Example:
- var PlaceGroupData = (_friends.GroupBy(s => s.Place).ToArray());
- _friendsList is collection of LIST records, where I filtered PLACE=JODHPUR records.
- var JodhpurFriends = (_friendsList.Where(x => x.Place == "Jodhpur").ToList<Friend>());
CONSOLE APPLICATION CODE
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace LinqIntro
- {
- class Program
- {
- static void Main(string[] args)
- {
- Friend[] _friends =
- {
- new Friend
- {
- FriendID = 1, FriendName = "Rajesh", MobileNumber = "982111111", Place = "Mumbai", Technology = ".NET"
- },
- new Friend
- {
- FriendID = 2, FriendName = "Mahesh", MobileNumber = "982122222", Place = "Mumbai", Technology = ".NET"
- },
- new Friend
- {
- FriendID = 3, FriendName = "Suresh", MobileNumber = "982133333", Place = "Mumbai", Technology = "JAVA"
- },
- new Friend
- {
- FriendID = 4, FriendName = "Jayesh", MobileNumber = "982144444", Place = "Mumbai", Technology = ".NET"
- },
- new Friend
- {
- FriendID = 5, FriendName = "Pritesh", MobileNumber = "98115555", Place = "Jodhpur", Technology = ".NET"
- },
- new Friend
- {
- FriendID = 6, FriendName = "Mayuresh", MobileNumber = "98116666", Place = "Jodhpur", Technology = "JAVA"
- },
- new Friend
- {
- FriendID = 7, FriendName = "Tarunesh", MobileNumber = "981177777", Place = "Jodhpur", Technology = ".NET"
- },
- new Friend
- {
- FriendID = 8, FriendName = "Ramesh", MobileNumber = "981188888", Place = "Phalodi", Technology = "JAVA"
- },
- new Friend
- {
- FriendID = 9, FriendName = "Kamlesh", MobileNumber = "981199999", Place = "Phalodi", Technology = ".NET"
- }
- };
-
-
- Friend[] MumbaiFriends = (_friends.Where(x => x.Place == "Mumbai").ToArray());
-
- Console.WriteLine("Aaray Working Example");
- Console.WriteLine("=====================");
- Console.WriteLine("Printing Mumbai Friends.");
- foreach(Friend frnd in MumbaiFriends)
- {
- Console.WriteLine(frnd.FriendID.ToString() + " " + frnd.FriendName + " " + frnd.Place + " " + frnd.Technology);
- }
- Console.ReadKey();
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine("Printing DOT NET Technology Friends.");
-
- Friend[] DotNetFriends = (_friends.Where(x => x.Technology == ".NET").ToArray());
-
- foreach(Friend frnd in DotNetFriends) {
- Console.WriteLine(frnd.FriendID.ToString() + " " + frnd.FriendName + " " + frnd.Place + " " + frnd.Technology);
- }
- Console.ReadKey();
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine();
-
- var PlaceGroupData = (_friends.GroupBy(s => s.Place).ToArray());
- Console.WriteLine("Place ----- Nos. of Friends");
- foreach(var Place in PlaceGroupData)
- {
- Console.WriteLine(Place.Key.ToString() + " ----- " + Place.Count().ToString());
- }
- Console.ReadKey();
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine();
-
-
- Console.WriteLine("List Working Example");
- Console.WriteLine("=====================");
- List < Friend > _friendsList = new List < Friend > ()
- {
- new Friend
- {
- FriendID = 1, FriendName = "Rajesh", MobileNumber = "982111111", Place = "Mumbai", Technology = ".NET"
- },
- new Friend
- {
- FriendID = 2, FriendName = "Mahesh", MobileNumber = "982122222", Place = "Mumbai", Technology = ".NET"
- },
- new Friend
- {
- FriendID = 3, FriendName = "Suresh", MobileNumber = "982133333", Place = "Mumbai", Technology = "JAVA"
- },
- new Friend
- {
- FriendID = 4, FriendName = "Jayesh", MobileNumber = "982144444", Place = "Mumbai", Technology = ".NET"
- },
- new Friend
- {
- FriendID = 5, FriendName = "Pritesh", MobileNumber = "98115555", Place = "Jodhpur", Technology = ".NET"
- },
- new Friend
- {
- FriendID = 6, FriendName = "Mayuresh", MobileNumber = "98116666", Place = "Jodhpur", Technology = "JAVA"
- },
- new Friend
- {
- FriendID = 7, FriendName = "Tarunesh", MobileNumber = "981177777", Place = "Jodhpur", Technology = ".NET"
- },
- new Friend
- {
- FriendID = 8, FriendName = "Ramesh", MobileNumber = "981188888", Place = "Phalodi", Technology = "JAVA"
- },
- new Friend
- {
- FriendID = 9, FriendName = "Kamlesh", MobileNumber = "981199999", Place = "Phalodi", Technology = ".NET"
- }
- };
-
-
- var JodhpurFriends = (_friendsList.Where(x => x.Place == "Jodhpur").ToList < Friend > ());
- Console.WriteLine("Printing Jodhpur Friends.");
- foreach(Friend frnd in JodhpurFriends)
- {
- Console.WriteLine(frnd.FriendID.ToString() + " " + frnd.FriendName + " " + frnd.Place + " " + frnd.Technology);
- }
- Console.ReadKey();
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine();
- Console.WriteLine("Printing DOT NET Technology Friends.");
-
- var JavaFriends = (_friends.Where(x => x.Technology == "JAVA").ToList < Friend > ());
-
- foreach(Friend frnd in JavaFriends)
- {
- Console.WriteLine(frnd.FriendID.ToString() + " " + frnd.FriendName + " " + frnd.Place + " " + frnd.Technology);
- }
- Console.ReadKey();
- Console.WriteLine();
-
- }
- }
-
- public class Friend
- {
- public int FriendID
- {
- get;
- set;
- }
- public string FriendName
- {
- get;
- set;
- }
- public string MobileNumber
- {
- get;
- set;
- }
- public string Place
- {
- get;
- set;
- }
- public string Technology
- {
- get;
- set;
- }
- }
-
- }