Language-Integrated Query (LINQ) in C# allows data manipulation in the form of queries similar to SQL ones. It enables data in data stores to be treated as objects, which can then be retrieved via queries.
The data stores on which LINQ is applicable include the following: collections, ADO.net, databases, web services, and XML docs.
The following lines represent how LINQ can be used in C#:
from <var> in <collection>:<Standard Query Operator><expression><Selection Operator><result>
A loop traverses through the data collection, and a range variable is used to track which data record is currently in use. Then, an SQL operator is used to retrieve the data according to some condition that comes in the expression clause.
The result is then returned via a selection operator. The selection operator could be SELECT
or GROUPBY
.
The following code has a LINQ query in it:
using System;using System.Linq;using System.Collections.Generic;class HelloWorld{static void Main(){IList<string> data = new List<string>(){"Pop","Stack","Queue","List","Data structures"};var result = from x in datawhere x.Contains("a")select x;foreach(string res in result){Console.WriteLine(res);}}}
Lines 8–14: We create a data collection stored in a List
structure with multiple string objects.
Lines 15–17: We create a LINQ query to get all the string objects from the list with an "a" in them. The result
variable has the objects returned by the LINQ query.
Lines 18–20: We create a loop to print the result of the LINQ query.
The following are some examples of LINQ queries that we can use in C#:
We can manipulate anonymous types using LINQ query:
using System;using System.Linq;class HelloWorld{class Person{public int id;public string name;public string occupation;public string dob;public Person(){id = 0;name = "";occupation = "";dob = "";}public Person(int x, string n, string o, string d){id = x;name = n;occupation = o;dob = d;}}static void Main(){Person[] p_array = {new Person(1, "John", "Manager", "2001-04-01"),new Person(2, "Lenny", "Lawyer", "1997-12-11"),new Person(3, "Andrew", "Manager", "1987-02-22"),new Person(4, "Peter", "Lawyer", "1936-03-24"),new Person(5, "Anna", "Lawyer", "1973-11-18")};var res = from person in p_arraywhere person.occupation == "Lawyer"select new { person.name, person.occupation };Console.WriteLine(string.Join(",",res));}}
OrderBy
queryThe OrderBy
clause is also present in the LINQ query, and we can implement it in this way:
using System;using System.Linq;using System.Collections.Generic;class HelloWorld{static void Main(){IList<string> data = new List<string>(){"Pop","Stack","Queue","List","Data structures"};var result = (from x in datawhere x.Contains("a")//OrderBy clause to//get data according to//alphabetic orderselect x).OrderBy(x=>x);foreach(string res in result){Console.WriteLine(res);}}}
group
clauseWe also have the group
clause in LINQ:
using System;using System.Linq;class HelloWorld{class Person{public int id;public string name;public string occupation;public string dob;public Person(){id = 0;name = "";occupation = "";dob = "";}public Person(int x, string n, string o, string d){id = x;name = n;occupation = o;dob = d;}}static void Main(){Person[] p_array = {new Person(1, "John", "Manager", "2001-04-01"),new Person(2, "Lenny", "Lawyer", "1997-12-11"),new Person(3, "Andrew", "Manager", "1987-02-22"),new Person(4, "Peter", "Lawyer", "1936-03-24"),new Person(5, "Anna", "Lawyer", "1973-11-18")};//LINQ query where groups are made using//occupations of the personsvar groupByOccupation = from person in p_arraygroup person by person.occupation into gselect g;//Printing each group's itemsforeach (var g in groupByOccupation){Console.WriteLine($"Key: {g.Key}");foreach (var person in g){Console.WriteLine($"\t{person.name}, {person.occupation}");}}}}
LINQ has the following benefits:
Code effortlessly: There is IntelliSense support for LINQ. This increases the productivity of developers.
Umbrella syntax: Developers do not need to learn a new SQL language for different types of data collections.
Readability: It makes the code much more readable.
Data manipulation: Data objects can be shaped into different forms using LINQ queries.
Free Resources