LINQ Learning-Summary after first reading

Source: Internet
Author: User

Today will buy before the "ASP." NET Advanced Programming fourth Edition "took out to look at, where the 13th chapter about the LINQ technology, but also good, before oneself for this piece belongs to only smell its name do not know its rationale, so in order to maintain their knowledge of the endless bitter boat mentality, the content of their study today to share the summary, For friends like me who need to be exposed to learning LINQ, they can also offer some help.

(1) What is LINQ technology?

This is in the ASP. NET Advanced Programming fourth edition of the first section of this article explains the origins of this technology in the ASP. Linq is a new technology that was introduced in ASP. 3.5 with Ajax in the original version 2.0.

Next is the 13 chapters in some of the knowledge points combined with my own understanding, which I myself also exist two doubts, if there is proficiency in the hope to guide a, haha, into the topic:

LINQ: Abbreviations are language Integrated Query language integrated queries, is a technology to manipulate the memory data, after reading a section, the feeling and SQL query is the difference is that it can be some data class object execution query filtering, return their requested data, that is, it can implement both C #源代码环境中的对象数据查询, you can also implement relational database data access.

LINQ technology provides five more useful types of data access for our developers:

    • LinQ to object: You can allow querying of class objects in memory.
    • LinQ to DataSet: You can cache data in an in-memory dataset and perform data access.
    • LinQ to xml: A parsing wrapper for XML data enables traditional XML parsing effects.
    • LINQ to Entity: This is one of the highlights of the current LINQ technology epidemic, which provides data access to relational databases, which allows developers to implement database access without having to write the data Access layer responsible for ADO, or to combine them together. What are the advantages of LINQ relative to ADO? Does it really have such powerful data access capabilities as ADO? This is my first question today, perhaps still need to learn in the back to understand.
    • LINQ to SQL: This is now replaced by LINQ to entity because it restricts only SQL Server databases.

(2) How does LINQ technology develop and implement?

    • LINQ Expressions: Like we need to write SQL statements to query data, it is natural to use a "statement", which is a LINQ expression, and it has its own syntax rules as well as SQL statements. It also has some keywords similar to those in the SQL statement: the select where the GroupBy is already similar to the syntax. For a moment, in my own verification example, I'll make an example of this.
    • The LINQ expression return value must be an iterative object that implements the ienumerable<t>.
    • When an iteration object is enumerated, LINQ performs its work.

(3) deferred execution of LINQ: The process of performing a return in a LINQ expression, the characteristic of deferred execution described in the book, may be that, depending on the type of resolution, LINQ may be executed one time or incrementally during iteration. But the concept is still vague, this is my second question, but also need to go into the study of the time to review.

(4) Several core features of the LINQ expression: in order to make it easier to understand the following sections will be illustrated with their own subsequent program validation to illustrate the features

I defined the data class first:

//Defining data Classes     Public classMytestdata { Public intStudentID {Set;Get; }        List binding the GridView listing property cannot be read-only or an error is.  Public stringName {Set;Get; }  Public intage{Set;Get;}  PublicMytestdata (intIdstringNameintAge ) {             This. StudentID =ID;  This. Name =name;  This. Age =Age ; }           }

Initialize the test data in the page Page_Load, originally thinking that since it is a query object data collection, it defines a ArrayList to load its own definition of the data class, when writing LINQ expressions, found a problem:

A custom data class container requires an implementation of the query pattern, so that is, LINQ is a query that supports a subset of the data types ....

The solution is to use the list type:

  New List<mytestdata> ();

Let's look at a simple example of a LINQ expression:

 protected voidPage_Load (Objectsender, EventArgs e) {            //defining test validation dataList<mytestdata> MyData =NewList<mytestdata>(); MyData. ADD (NewMytestdata (1,"George", at)); MyData. ADD (NewMytestdata (2,"Lio", -)); MyData. ADD (NewMytestdata (3,"Kaiwen", -)); MyData. ADD (NewMytestdata (4,"Anna", +)); MyData. ADD (NewMytestdata (5,"Angel", -)); MyData. ADD (NewMytestdata (6,"Geo", -)); MyData. ADD (NewMytestdata (7,"Demo", -)); MyData. ADD (NewMytestdata (8,"haha", A)); //1. The simplest implementation of LINQ expressionsIenumerable<mytestdata>Matchs; Matchs= fromStudentinchMyData//student is the kana that queries the objects in the MyData collection                     whereStudent.age> -    //Query Filter Criteria                     SelectStudent//query returns a collection of Matchs that meet the filter criteria//page binding Data displayGridview1.datasource =Matchs;        Gridview1.databind (); }

Debug to see the matching data types returned:

Page effect:

We should have a preliminary understanding of LINQ expressions, and now combine some examples to illustrate several of the effects that LINQ expressions can achieve:

    • Projection: In short, the SELECT statement supports some data types and string data operations, and can even dynamically define a new class return information, similar to the one in our previous SQL statement, where a LINQ expression can return data to a query, Some operations are supported to return the type that we expect, a string, or a dynamically created class.

However, there are some differences in the expressions for the general value type operation and the projection of the custom return object, and now an example is shown:

            //2. Projection-value type//Note: Matchs is declared in this ienumerable<string> for the string type, indicating that the iteration object that returned the stringienumerable<string>Matchs; Matchs= fromStudentinchMyData//student is the kana that queries the objects in the MyData collection                     whereStudent.age > -    //Query Filter Criteria                     SelectStudent.name +"the added characters";//query returns a collection of Matchs that meet the filter criteria//page binding Data displayGridview1.datasource =Matchs; Gridview1.databind ();
            //2. Projection--Object type//Note: Matchs is declared in this ienumerable<string> for the string type, indicating that the iteration object that returned the string//ienumerable<string> matchs;            varMatchs = fromStudentinchMyData//student is the kana that queries the objects in the MyData collection                         whereStudent.age > -    //Query Filter Criteria//the new{} Here is an implicitly created class object that does not have an established type, so it cannot be passed ienumerable< class name > Matchs//to match the returned iteration class object, but the type of the object can be returned through VAR or earlier definition                         Select New{id=student.studentid,name=student.name,age=Student.age}; //page binding Data displayGridview1.datasource =Matchs; Gridview1.databind ();
    • Filtering and Sorting: The point of comparison is that the where statement can be used with logical expressions and multiple conditional expressions in the SQL syntax, most notably because they are in the C # source environment so we can call ourselves to customize one method such as where MyFunction (Class object property value)
            //3 filtering and sortingIenumerable<mytestdata>Matchs; Matchs= fromStudentinchMyData//student is the kana that queries the objects in the MyData collection                     whereStudent.age > -    //Query Filter Criteria                      byStudent.age//Sort                     Selectstudent; //page binding Data displayGridview1.datasource =Matchs; Gridview1.databind ();
    • Grouping and aggregation: If the returned data is grouped and the Ienumerable<t> collection of grouped objects is returned, each group implements the Igrouping<t,k> interface, first we need to determine the grouping criteria, and next we need to determine what information each group needs to return.
             //3 grouping and aggregation            varMatchs = fromStudentinchMyData//student is the kana that queries the objects in the MyData collection                         whereStudent.age > -    //Query Filter Criteria                          byStudent.age//SortGroup student by Student.age into G//G is an iterative igouping<t,k> object, and each group is a Ienumerable<mytestdata> object                         Select New{age = G.key, Avergeage = g.average (student =student.age)}; //page binding Data displayGridview1.datasource =Matchs; Gridview1.databind ();

The final:

Unconsciously all three hours, too late, today summed up here ....

LINQ Learning-Summary after first reading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.