LINQ introduction
What problems can LINQ solve
In the development of NET platform, object-oriented programming language and data access method are separated for a long time. Data access is queried through SQL. This way of embedding SQL statements is very prone to errors.
SQL and XML have their own query language, while objects do not have their own query language.
For another example, it is very difficult to find qualified elements from the list < T > set or array.
What is LINQ
LINQ (Language Integrated Query)
LINQ mainly includes the following three parts:
- LINQ to Objects is mainly responsible for object query;
- LINQ to XML is mainly responsible for XML query;
- LINQ to ADO.NET is mainly responsible for database query;
- (1)LINQ to SQL;
- (2)LINQ to DataSet;
- (3)LINQ to Entities;
LINQ namespace
- SyStem.Linq; This namespace is automatically introduced by the system.
Comparison between using and not using LINQ queries
-
Do not use LINQ
#region example 1: do not use LINQ query int[] nums = { 1, 8, 2, 321, 82, 93, 288, 29 }; List<int> list = new List<int>(); foreach (int item in nums) { if (item %2 !=0) { list.Add(item); } } list.Sort(); list.Reverse(); foreach (int item in list) { Console.WriteLine(item); } Console.ReadLine(); #endregion
-
Using LINQ queries
#region example 2: querying an array using LINQ Technology int[] nums = { 1, 8, 2, 321, 82, 93, 288, 29 }; var list = from num in nums where num % 2 != 0 orderby num descending select num; foreach (int item in list) { Console.WriteLine(item); } Console.ReadKey(); #endregion
LINQ query is simple, intuitive and clear.
LINQ query method
-
Get data: extension method (select)
- Select() is a generic extension method. In the select method, there is a Lambda expression, and the return result is an Iterator
- When the Select() method is used, it is required to pass a delegate instance (the delegate instance is a method)
Select() method application
#region example 3: Select() method application int[] nums = { 1, 8, 2, 321, 82, 93, 288, 29 }; var list = nums.Select(item => item * item); foreach (int item in list) { Console.WriteLine(item); } Console.ReadKey(); #endregion
-
Filter data: Where() method
- The Where() method is a generic extension method
- When the Where() method is used, it is required to pass a delegate real column, but the instance is a judgment condition, so the returned type must be bool type
Where() method application
#region example 4: Where() method application int[] nums = { 1, 8, 2, 321, 82, 93, 288, 29 }; var list = nums.Where(item => item % 2 == 0).Select(i => i * i); foreach (int item in list) { Console.WriteLine(item); } Console.ReadKey(); #endregion
-
Sorting data: OrderBy() method
- OrderBy() is an extension method
- The parameters in OrderBy() require a sorted field to be passed, which is sorted in ascending order by default
- If you want to arrange in descending order, you can use the OrderByDescending method
Application of OrderBy() method
#region example 5: application of OrderBy() method int[] nums = { 1, 8, 2, 321, 82, 93, 288, 29 }; var list = nums .Where(item => item % 2 == 0) .Select(i => i * i) .OrderByDescending(item => item); foreach (var item in list) { Console.WriteLine(item); } Console.ReadKey(); #endregion
String array sort:
#region example 6: string sorting string[] nums = { "Zhang San", "Li Si", "Wang wuqi", "Chen Liu", "Qian Qi", "Zhao Ba" }; var list = nums .Where(item => item.Length == 2) .Select(item => item) .OrderBy(item => item.Substring(0, 1)); foreach (var item in list) { Console.WriteLine(item); } Console.ReadLine(); #endregion
-
Grouping data: GroupBy() method
- OrderBy() is an extension method
- The parameters in OrderBy() require a grouped field to be passed from
GroupBy() method application
#region Example 7: GroupBy() method application string[] nums = { "Zhang San", "Li Si", "Wang wuqi", "Chen Liu", "Qian Qi", "Zhao Ba" }; var list = nums .Where(item => item.Length == 2) .Select(item => item) .GroupBy(item => item.Substring(0, 1)); foreach (var groupitem in list) { Console.WriteLine("---------------"); Console.WriteLine("Group fields:{0}", groupitem.Key); foreach (var item in groupitem) // Inner loop traversal grouping item { Console.WriteLine(item); } } Console.ReadLine(); # endregion
-