concept
Language Integrated Query (LINQ) is a collective name for a series of technologies that directly integrate query functions into the C# language.
Data queries have traditionally been expressed as simple strings without compile-time type checking or IntelliSense support. In addition, you need to understand different query languages for each type of data source: SQL database, XML documents, various web services, etc.
With LINQ, queries become the most advanced language construct, just like classes, methods and events. Queries can be written for strongly typed object collections using language keywords and familiar operators. The LINQ family of technologies provides a consistent query experience for objects (LINQ to Objects), relational databases (LINQ to SQL), and XML (LINQ to XML).
For developers writing queries, the most obvious "language integration" part of LINQ is query expressions.
Query expressions are written in declarative query syntax. Using query syntax, filtering, sorting, and grouping operations can be performed on data sources with minimal code. The same basic query expression pattern can be used to query and transform data from SQL databases, ADO .NET datasets, XML documents and streams, and .NET collections.
In C#, you can write LINQ queries for SQL Server databases, XML documents, datasets, and any collection of objects that support the IEnumerable or generic IEnumerable interface. In addition, third parties also provide LINQ support for many web services and other database implementations.
Implementation case
using System; using ; using ; using ; using ; namespace LinkConsole { class Program { static void Main(string[] args) { //-------------------------------------// List<int> numbers = new List<int>() { 1,2,3,4,5,6,7,8,9,10}; var numQuery = from num in numbers where num % 2 == 0 select num; foreach (var num in numQuery) { ("{0,1}", num); } //--------------------------------------------// FormExpDemo2(); //----------------------------// FormExpDemo(); //-----------------------------------// FormExpDemo3(); //-------------where-------------------// WhereExpDemo(); //-------------select------------------// SelectDemo(); //-------------group--------------------// GroupDemo(); //-------------into------------------------// IntoDemo(); //--------------OrderBy--------------------// ThenByDemo(); //--------------let----------------------// LetDemo(); //--------------join--------------------// JoinDemo(); (); } public class CustomerInfo { public string Name { get; set; } public int Age { get; set; } public string Tel { get; set; } public List<string> telTable { get; set; } } public static void FormExpDemo2() { // List<CustomerInfo> customers = new List<CustomerInfo> { new CustomerInfo{ Name = "Ouyang Xiaoxiao",Age = 35,Tel = "123"}, new CustomerInfo{ Name = "Shangguan Piaopiao",Age = 17,Tel = "456"}, new CustomerInfo{ Name = "Zhuge Feifei",Age = 23,Tel = "789"} }; var query = from ci in customers where > 20 select ci; foreach (var ci in query) { ("Name:{0}age:{1}Telephone:{2}", , , ); } } //Compound from clause // It is equivalent to two for loops private static void FormExpDemo() { List<CustomerInfo> customers = new List<CustomerInfo> { new CustomerInfo { Name = "Ouyang Xiaoxiao",Age= 35,telTable = new List<string> {"123","234"} }, new CustomerInfo { Name = "Shangguan Piaopiao",Age= 35,telTable = new List<string> {"456","567"} }, new CustomerInfo { Name = "Zhuge Feifei",Age= 35,telTable = new List<string> {"789","456"} }, }; //Inquiry of customers with phone number 456 var query = from ci in customers from tel in where ("456") > -1 select ci; foreach (var ci in query) { ("Name:{0}age:{1}", , ); foreach (var tel in ) { (" Telephone:{0}", tel); } } } // Multiple from clauses look the same as composite clauses, but they are actually different. One is a collection of child elements in a single data source, and the other is a query for multiple data sources. private static void FormExpDemo3() { List<CustomerInfo> customers = new List<CustomerInfo> { new CustomerInfo{ Name = "Ouyang Xiaoxiao",Age = 35,Tel = "123"}, new CustomerInfo{ Name = "Shangguan Piaopiao",Age = 77,Tel = "456"}, new CustomerInfo{ Name = "Zhuge Feifei",Age = 23,Tel = "789"} }; List<CustomerInfo> customers2 = new List<CustomerInfo> { new CustomerInfo{ Name = "Linghu Chong",Age = 25,Tel = "123"}, new CustomerInfo{ Name = "The East is Unbeaten",Age = 15,Tel = "456"}, new CustomerInfo{ Name = "Ren Yingying",Age = 13,Tel = "789"} }; //Look for customers older than 20 // Find customers under 30 in customerrs var query = from custo in customers where > 20 from custo2 in customers2 where < 30 select new { custo, custo2 }; foreach (var ci in query) { ("{0},{1}", , ci.);//What I get in this way is a cross-link table, which is somewhat similar to Cartesian deposition in SQL } } // Where clause query //where is used to filter elements. In addition to the start and end positions, where can be used at any position. //A LIKQ statement can have a where clause, or there can be no, there can be one, or there can be multiple. //The relationship between multiple where clauses is equivalent to logical "Ans", and each clause can also contain multiple logical expressions linked with "predicates", &&, or || private static void WhereExpDemo() { List<CustomerInfo> clist = new List<CustomerInfo> { new CustomerInfo{ Name="Ouyang Xiaoxiao", Age=35, Tel ="1330708****"}, new CustomerInfo{ Name="Shangguan Piaopiao", Age=17, Tel ="1592842****"}, new CustomerInfo{ Name="Linghu Chong", Age=23, Tel ="1380524****"} }; //You can query people who meet multiple conditions (the name is three characters or surname is ordered, but the age must be greater than 20) var query = from custo in clist where ( == 3 || (0, 1) == "make") && > 20 select custo;//select can also be changed to, for example. Or use a function to pass the variable out foreach (var ci in query) { ("Name:{0}age:{1}Telephone:{2}", , , ); } // Use a custom function in where to query the client with three characters and surname order var query2 = from custo in clist where ( == 3 && ChechName()) select custo; foreach (var ci in query2) { ("Name:{0}age:{1}Telephone:{2}", , , ); } } private static bool ChechName(string name) { if ((0, 1) == "make") return true; else return false; } //Select usage example private static void SelectDemo() { List<CustomerInfo> clist = new List<CustomerInfo> { new CustomerInfo{ Name="Ouyang Xiaoxiao", Age=35, Tel ="1330708****"}, new CustomerInfo{ Name="Shangguan Piaopiao", Age=17, Tel ="1592842****"}, new CustomerInfo{ Name="Linghu Chong", Age=23, Tel ="1380524****"} }; string[] names = { "Linghu Chong", "Ren Yingying", "Yang Guo", "Little Dragon Girl", "Ouyang Xiaoxia", "Ouyang Xiaoxiao" }; //Query the client that exists in the given predicate array var query = from custo in clist where < 30 select new MyCustomerInfo { Name = , Tel = }; foreach (var ci in query) { ("Name:{0}Telephone:{1}type{2}", , , ().FullName); } } public class MyCustomerInfo { public string Name { get; set; } public string Tel { get; set; } } //-------------------Group----------------------// static List<CustomerInfo> clist = new List<CustomerInfo> { new CustomerInfo{ Name="Ouyang Xiaoxiao", Age=35, Tel ="1330708"}, new CustomerInfo{ Name="Shangguan Piaopiao", Age=17, Tel ="1592842"}, new CustomerInfo{ Name="Ouyang Jinpeng", Age=35, Tel ="1330708"}, new CustomerInfo{ Name="Shangguan Wuji", Age=23, Tel ="1380524"} }; private static void GroupDemo() { //Group according to the first two words of the name var query = from custo in clist group custo by (0, 2); foreach (IGrouping<string, CustomerInfo> group in query) { ("Grouping keys:{0}", ); foreach (var ci in group) { ("Name:{0}Telephone:{1}", , ); } ("*********************"); } //You can know that the group clause returns an object collection of IGrouping<TKey,TElement> generic interface //TKey is the object type of the key. When used in the group clause, the compiler will recognize the data type and use it to store the key value of the group, that is, the group based on which group it is divided //TElement refers to the object type used to allocate and store the result. The type of the variable based on this interface is to traverse this value, that is, the grouped objects } //-----------------------------------// private static void IntoDemo() { //into provides a temporary identifier, which stores the query content before the into clause, so that the clause behind it can be used easily, query the projection again var query = from custo in clist group custo by (0, 2) into gpcustomer orderby descending //Sort, select gpcustomer; ("into is used for group clause"); foreach (var group in query) { ("See grouping:{0}", ); foreach (var ci in group) { ("Name:{0}Telephone:{1}", , ); } ("***********************"); } var query2 = from custo in clist select new { NewName = , NewAge = } into newCustomer orderby select newCustomer; ("into is used for select clause"); foreach (var ci in query2) { ("{0}age:{1}", , ); } } //------------------------------------// //LINQ can sort elements by one or more attributes of the element. The sorting methods of expressions are divided into OrderBy, OrderByDescending, ThenBy, ThenByDescending //The ones that add Descending are descending order, and the ones that do not add are ascending order private static void ThenByDemo() { List<CustomerInfo> clist = new List<CustomerInfo> { new CustomerInfo{ Name="Ouyang Xiaoxiao", Age=35, Tel ="1330708****"}, new CustomerInfo{ Name="Shangguan Piaopiao", Age=17, Tel ="1592842****"}, new CustomerInfo{ Name="Guo Jing", Age=17, Tel ="1330708****"}, new CustomerInfo{ Name="Huang Rong", Age=17, Tel ="1300524****"} }; //Ascending order according to age, then sort it according to the number of words in the name var query = from customer in clist orderby , select customer; ("Sort by age, sort secondary by number of words"); foreach (var ci in query) { ("Name:{0} age:{1} Telephone:{2}",, , ); } //According to descending order by age, then in descending order of the number of words in the name var query2 = from customer in clist orderby descending , descending select customer; ("\nArrange by age, and arrange in descending order by number of names and words"); foreach (var ci in query2) { ("Name:{0} age:{1} Telephone:{2}", , , ); } } //----------------------------------// private static void LetDemo() { var query = from custo in clist let g = (0, 1)// Let create a scope variable, use it in where where g == "Europe" || g == "superior"//You can also not write it, write it as (0, 1) == "Guo" || (0, 1) == "Yellow" select custo; foreach (var ci in query) { ("Name:{0} age:{1} Telephone:{2}", , , ); } } //-----------------------------------// private static void JoinDemo() { //If the properties in two data sources can be compared equally, then the two sentences can be associated with join, and the comparison symbol is equal, instead of == List<CustomerInfo> clist = new List<CustomerInfo> { new CustomerInfo{ Name="Ouyang Xiaoxiao", Age=35, Tel ="1330708****"}, new CustomerInfo{ Name="Shangguan Piaopiao", Age=17, Tel ="1592842****"}, new CustomerInfo{ Name="Guo Jing", Age=17, Tel ="1330708****"}, new CustomerInfo{ Name="Huang Rong", Age=17, Tel ="1300524****"} }; List<CustomerTitle> titleList = new List<CustomerTitle> { new CustomerTitle{ Name="Ouyang Xiaoxiao", Title="singer"}, new CustomerTitle{ Name="Guo Jing", Title="The Hero"}, new CustomerTitle{ Name="Guo Jing", Title="Hong Qigong's apprentice"}, new CustomerTitle{ Name="Huang Rong", Title="Talented Girl"}, new CustomerTitle{ Name="Huang Rong", Title="The leader of the Beggars' Sect"} }; //Internal connections are made based on the name var query = from customer in clist join title in titleList on equals select new { Name = , Age = , Title = }; foreach (var ci in query) { ("Name:{0} age:{1}{2}", , , ); } //Group by name ("\nGroup by name"); var query2 = from customer in clist join title in titleList on equals into tgroup select new { Name = , Titles = tgroup }; foreach (var g in query2) { (); foreach (var g2 in ) { (" {0}", ); } } //Add to the left external link based on the name ("\nLeft external connection"); var query3 = from customer in clist join title in titleList on equals into tgroup from subTitle in () select new { Name = , Title = (subTitle == null ? "vacancy" : ) }; foreach (var ci in query3) { ("Name:{0} ", , ); } } public class CustomerTitle { public string Name { get; set; } public string Title { get; set; } } } }
This is all about this article about the use case of LINQ (Language Integration Query). I hope it will be helpful to everyone's learning and I hope everyone will support me more.