First, this series of goals
1, understand LINQ;
2, can write complex LINQ statements (such as: Dynamic query);
3, understand the expression tree and related concepts;
4, skilled use of LINQ to write beautiful code (hope to work together, and eventually achieve);
Second, what is LINQ?
LINQ's Battle of cigarettes has receded, and today, LINQ has become one of the must-have technologies for C # developers. Many people use it to write beautiful code, which has become a new way to develop data processing, which may be your choice. NET as one of the benefits of the development platform. More and more open source libraries, frameworks are heavily used in LINQ. Whether it's about improving your skills or reading someone else's code, it has to be a stone that you and I have taken down. Next I will introduce it to you, and talk about some of your own understanding, if there is inappropriate place to welcome correct.
1. What is LINQ?
A common, language-Integrated query tool. With this tool, you can access data from a variety of data sources such as memory Objects (LINQ to Objects), Databases (LINQ to SQL), XML Files (LINQ to XML), file systems, and so on. It is a set of features introduced in Visual Studio 2008 that provides powerful query capabilities for C # and Visual Basic language syntax, and has withstood the test and precipitation of time.
The following is the traditional method of querying data in a Memory object data source and how LINQ is written:
<summary>///elements less than 5 in the output array///</summary> public static void Rraditionalmetho D () {int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; The traditional method uses circular mode list<int> lownums = new list<int> (); for (int i = 0; i < numbers. Length; i++) {if (Numbers[i] < 5) {Lownums.add (numbers[i]); }} Console.WriteLine ("Numbers < 5:"); foreach (var x in lownums) {Console.WriteLine (x); }}///<summary>///The element less than 5 in the output array///</summary> public static void Linqme Thod () {int[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}; LINQ notation var lownums = from num in numbers where num < 5 select Num Console.WriteLine ("Numbers < 5:"); foreach (var x in lownums) { Console.WriteLine (x); } }
Maybe you think that the LINQ method of querying data in a Memory object's data source doesn't give me any benefit (at least from the example, the number of code is almost). Don't worry! Let's take a look at the operation of the data in the database, and then we'll discuss it, before we query the database (specifically the relational database) for persisted data in the SQL statement, the red (string in VS color) SQL statement will certainly bring you a lot of trouble before LINQ. Maybe it's a misspelled letter, maybe a typo, maybe ..., the compiler didn't give us any hints. The result is a run-time exception. In a complex system, it is normal to take a few hours to solve the problem, and worse, the same error often happens!!! But with LINQ, we're using LINQ to SQL, and that's not going to bother you anymore. Take a look at the example below to see if you are not in the heartbeat.
<summary>///Enquiry order number less than 5 traditional notation///</summary>//<param name= "connectionString "></param> private static void Readorderdata (String connectionString) {String queryString = "SELECT * FROM dbo. Orders WHERE orderid<5; "; using (SqlConnection connection = new SqlConnection (connectionString)) {SqlCommand Command = new SqlCommand (queryString, connection); Connection. Open (); SqlDataReader reader = command. ExecuteReader (); try {while (reader. Read ()) {Console.WriteLine (String.Format ("{0}, {1}", Reader[0], reader[ 1])); }} finally {//close object reader after reading. Close (); } } }
<summary>///Enquiry order number less than 5 linqtosql notation/// </summary> public Void Readorderdatabylinqtosql () { var q = from C in db. Orders where C.orderid <5 select C; Objectdumper.write (q); }
Third, why to use LINQ
From the above comparison we can see that LINQ brings us the following benefits:
1, integration, it encapsulates the query into C # (vb.net) syntax, get the same as other C # (vb.net) syntax, highlighting, type checking.
2, use the query logic more clearly.
3, the use of unified query syntax processing memory objects, databases, XML and other data sources of data;
4, improve the development speed (above the LINQ to SQL example);
5, improve the maintainability of the Code, Extensibility (described later);
Outside, LINQ to SQL can effectively prevent SQL injection and never improve the security of the system. It is not early today, first wash and sleep in case of death. If you are helpful, please click on the recommended support to increase the motivation behind the writing. If there is anything wrong, please correct me again.
Step-by-step learning of the LINQ Series 1---What Is LINQ?