In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article, the editor introduces in detail "what LINQ is and how to use it". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "what LINQ is and how to use it" can help you solve your doubts.
First, why to use LINQ
To understand why you use LINQ, take a look at the following example. Suppose you have an array of integer types, find the even numbers in it and sort them in descending order.
Before Clover 2.0, to achieve this, we had to use the 'foreach' or' for' loop to iterate through the array, finding even numbers and then sorting them in descending order, with the following code:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LinqOfSelectOperation {class Program {static void Main (string [] args) {/ / query the even numbers in the array and sort int [] ints = {5, 2, 0, 66, 4, 32, 7, 1} / / defines a collection of integer types, which is used to store even numbers List list = new List () in the array. / / query the even number into the collection foreach (int i in ints) {/ / if it is an even number, add the even number to the collection if (I% 2 = = 0) {list.Add (I) }} / / sort list.Sort (); / / reverse list.Reverse (); / / output Console.WriteLine (string.Join (",", list)); Console.ReadKey ();}
Using for loops is cumbersome and unmaintainable and readable. Caching 2.0 introduces delegate, which can be used to handle this scenario, as shown in the following figure:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LinqOfSelectOperation {/ / define delegate delegate bool FindEven (int item); class IntExtension {public static int [] where (int [] array, FindEven dele) {int [] result=new int [5]; int I = 0 Foreach (int item in array) {if (dele (item)) {result [I] = item; iTunes;}} return result }} class Program {static void Main (string [] args) {/ / query the even numbers in the array and sort int [] ints = {5, 2, 0, 66, 4, 32, 7, 1}; / / delegate (int item) {return item% 2 = = 0 } indicates that the implementation of the delegate List list = IntExtension.where (ints, delegate (int item) {return item% 2 = = 0;}) .ToList (); / / positive sort list.Sort (); / / reverse list.Reverse () / / output Console.WriteLine (string.Join (",", list)); Console.ReadKey ();}
So, with Cure 2.0, you have the advantage of a proxy by using a delegate, instead of using a for loop to query arrays of different conditions. For example, you can use the same delegate to find the odd numbers in the array and sort the output in descending order, as shown in the following figure:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LinqOfSelectOperation {/ / define delegate delegate bool FindEven (int item); class IntExtension {public static int [] where (int [] array, FindEven dele) {int [] result=new int [3]; int I = 0 Foreach (int item in array) {if (dele (item)) {result [I] = item; iTunes;}} return result }} class Program {static void Main (string [] args) {/ / query the odd numbers in the array and sort int [] ints = {5,2,0,66,4,32,7,1}; / / delegate (int item) {return item% 2! = 0 } indicates that the implementation of the delegate List list = IntExtension.where (ints, delegate (int item) {return item% 2! = 0;}) .ToList (); / / positive sort list.Sort (); / / reverse list.Reverse () / / output Console.WriteLine (string.Join (",", list)); Console.ReadKey ();}
Although using delegate can make the program more readable, the C # team thinks they still need to make the code more compact and readable, so they introduced new features such as extension methods, Lambda expressions, anonymous types, and so on. You can use these new features of LINQ 3.0, which can be used to query different types of collections and return the desired results.
The following example shows how to query an array based on specific criteria using LINQ and Lambda expressions, with the sample code as follows:
Using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace LinqOfSelectOperation {class Program {static void Main (string [] args) {/ / query the odd numbers in the array and sort int [] ints = {5, 2, 0, 66, 4, 32, 7, 1} / query the even int [] intEvens= ints.Where (p = > p% 2 = = 0). ToArray () in the array using LINQ and Lambda expressions; / / query the odd int [] intOdds = ints.Where (p = > p% 2! = 0) in the array using LINQ and Lambda expressions. ToArray () / / output Console.WriteLine ("even:" + string.Join (",", intEvens)); Console.WriteLine ("odd:" + string.Join (",", intOdds)); Console.ReadKey ();}
As you can see in the above example, we use LINQ and Lambda expressions to specify different query conditions in a single statement, so LINQ makes the code more compact and readable, and it can also be used to query different data sources. When you see this, you may ask: what on earth is LINQ? The following will explain in detail what LINQ is.
What is LINQ
For a long time, the development community has formed the following pattern:
1. The two fields of object-oriented and data access have been separated for a long time. 2. The data types in the programming language form two different systems from those in the database, such as:
Strings in C # are represented by the string data type.
Strings in SQL are represented by the NVarchar/Varchar/Char data type.
3. SQL coding experience lags behind
No IntelliSense.
There is no strict sense of strong typing and type checking.
4. Both SQL and XML have their own query languages, while objects do not have their own query languages.
All the problems described above can be solved using LINQ, so what exactly is LINQ?
LINQ (Language Integrated Query) is language integrated query.
LINQ is a set of language features and API that allow you to write various queries in a uniform way. It is used to save and retrieve data from different data sources, eliminating the mismatch between programming languages and databases, and providing a single query interface for different types of data sources.
LINQ always uses objects, so you can use the same query syntax to query and transform data in XML, object collections, SQL databases, ADO.NET datasets, and any other available LINQ provider format.
LINQ consists of the following three parts:
1. LINQ to Objects is mainly responsible for querying objects.
2. LINQ to XML is mainly responsible for the query of XML.
3. LINQ to ADO.NET is mainly responsible for querying the database.
LINQ to SQL
LINQ to DataSet
LINQ to Entities
III. Advantages of LINQ
1. Familiar languages: developers do not have to learn a new language for each type of data source or data format.
2. Less coding: LINQ reduces the amount of code to be written compared to the traditional way.
3. Readability: LINQ increases the readability of the code, so other developers can easily understand and maintain it.
4. Standardized query method: multiple data sources can be queried using the same LINQ syntax.
5. Type checking: the program provides type checking at compile time.
6. IntelliSense hints: LINQ provides IntelliSense hints for general collections.
7. Plastic data: LINQ can retrieve data of different shapes.
After reading this, the article "what is LINQ and how to use it" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.