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());
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>());
- 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"
- }
- };
- // Filter the RecordSet
- 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();
- // Place Group By
- 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"
- }
- };
- // Filter the RecordSet
- 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;
- }
- }
- }

Sonu ChaudharyPosted May 26, 2016, 10:32 AM
good one
Bhuvanesh MohankumarPosted May 6, 2016, 3:53 PM
Good one...
Ammar ShaukatPosted Apr 4, 2016, 5:35 PM
nice
Abhilash J APosted Mar 6, 2016, 2:18 AM
Thank you for sharing sir, please continue your support!!!
Asfend YarPosted Mar 3, 2016, 3:34 PM
good
Santhakumar MunuswamyPosted Feb 3, 2016, 2:59 PM
Thanks for nice share. Kindly add source code here
Abhishek KumarPosted Feb 3, 2016, 4:46 AM
Good one sr..
Shubham KumarPosted Feb 3, 2016, 1:08 AM
nice one
Sibeesh VenuPosted Feb 3, 2016, 12:35 AM
Nice Share
Debasis SahaPosted Feb 2, 2016, 11:48 PM
Good One..
Hemant SrivastavaPosted Feb 2, 2016, 10:55 AM
Nice article ! Just wondering the examples/ sample code you gave in this article, they are related to Lambda expression not LINQ. May be I am missing something here..