Linq learning-Summary after the first reading, and summary after the first reading of linq

Source: Internet
Author: User

Linq learning-Summary after the first reading, and summary after the first reading of linq

 

ASP. the fourth edition of NET advanced programming design was taken out and reviewed. The section 13th on the description of the linq technology is not bad. I did not know the name of this section before, therefore, in order to keep yourself engaged in learning things, I will summarize and share my learning content today. Like me, my friends who need to get in touch with and learn linq can also provide some help.

(1) What is linq technology?

This is in ASP. the first section of NET advanced programming language version 4 explains the origins and backgrounds of this technology in asp.net. linq is a new technology introduced in asp.net 3.5 along with AJAX in the original version 2.0.

Next, I will discuss some of the knowledge points in chapter 13 in combination with my own understanding. I also have two questions. If I have the hope of being proficient, I will give some guidance. Haha, I will go to the topic:

Linq: language integrated query, is a technology used to operate on memory data. After reading a section, I feel that the difference from SQL query is that it can perform query filtering on some data objects, return the requested data, that is, it can query the object data in the c # source code environment or access the relational database data.

The linq technology provides five practical data access types for our developers:

  • LinQ to Object: allows you to query class objects in memory.
  • LinQ to DataSet: You can cache data in the DataSet in the memory and perform data access.
  • LinQ to xml: A parsing encapsulation for XML data can achieve the traditional xml parsing effect.
  • LinQ to Entity: This is one of the highlights of the current popularity of linq technology. It provides data access to relational databases, so that developers do not have to write ADO. the data access layer of. NET can be used together to access the database. What are the advantages of LinQ over ADO. NET? Does it really have such powerful data access functions as ADO. NET? This is my first question today. It may take some further learning to understand it.
  • LinQ to SQL: as only the SQL Server database is restricted, it has been gradually replaced by LinQ to Entity.

(2) how to develop and implement the LinQ technology?

  • LinQ expression: Like Data Query, We need to write an SQL statement. To use it, we naturally need a "statement", that is, a linq expression, which also has its own syntax rules like an SQL statement. It also has some keywords similar to those in SQL statements: select where orderby has already been groupby in terms of syntax. I will give an example in my own verification instance later.
  • The return value of a LinQ expression must be an iteration object that implements IEnumerable <T>.
  • When an iterative object is enumerated, linq executes its work.

(3) about the delayed execution of linQ: the features of the delayed execution described in the book about the process of execution return in the linQ expression only indicate that the latency may vary according to the resolution type, it may be a single execution or a step-by-step execution in the Process of iteration. However, I am still vague about this concept. This is my second question. I still need to review it in depth.

(4) several core features of the LinQ expression: to make it easier to understand the following features, we will explain the features in the examples of subsequent program verification.

I first defined the data class:

// Define the data class public class mytestData {public int studentid {set; get;} // The list attribute bound to the GridView list cannot be read-only. Otherwise, an error is returned. Public string name {set; get;} public int age {set; get;} public mytestData (int id, string name, int age) {this. studentid = id; this. name = name; this. age = age ;}}

 

Initialize the test data in page_load on the page. The original thought was to query the object data set, then define an ArrayList to load its own defined data class, A problem was found during the compilation of the LinQ expression:

Custom Data containers require the implementation of the query mode. Therefore, in other words, LinQ supports the query of some data types ....

The solution is to use the List type:

  List<mytestData> mydata = new List<mytestData>();

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

Protected void Page_Load (object sender, EventArgs e) {// define the test verification data List <mytestData> mydata = new List <mytestData> (); mydata. add (new mytestData (1, "george", 23); mydata. add (new mytestData (2, "lio", 25); mydata. add (new mytestData (3, "kaiwen", 20); mydata. add (new mytestData (4, "anna", 19); mydata. add (new mytestData (5, "angel", 16); mydata. add (new mytestData (6, "geo", 27); mydata. add (new mytestData (7, "demo", 30); mydata. add (new mytestData (8, "Haha", 22); // 1. the simplest implementation of the linq expression IEnumerable <mytestData> matchs; matchs = from student in mydata // student is the alias where student for querying objects in the mydata set. age> 20 // query the filter condition select student; // the query returns a set of matchs that meet the filter condition. // The data bound to the page displays GridView1.DataSource = matchs; GridView1.DataBind ();}

Check the returned matching data type for debugging:

 

Page effect:

We should have a preliminary understanding of the LinQ expression just now. Now we are combining some examples to illustrate the effects that the linQ expression 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, which is similar to some of the select statements in our previous SQL statements, the data returned by the query can be returned by a linQ expression. Some operations can be returned to the expected type, string, or dynamically created class.

However, there are still some differences between general value type operations and projection of custom returned objects in expressions. Here is an example:

// 2. Projection -- Value Type // Note: matchs has been declared as string type in IEnumerable <string>, indicating that the returned string iteration object IEnumerable <string> matchs; matchs = from student in mydata // student is the alias where student for querying objects in the mydata set. age> 20 // select student. name + "added characters"; // the query returns the set of matchs that meet the filtering conditions. // The page binds the data to display GridView1.DataSource = matchs; GridView1.DataBind ();
// 2. Projection -- Object Type // Note: matchs has been declared as string type in IEnumerable <string>, the returned result is the string iteration object // IEnumerable <string> matchs; var matchs = from student in mydata // student is the alias where student for querying objects in the mydata set. age> 20 // query filtering condition // here new {} is a class object created implicitly, and there is no set type, therefore, you cannot use IEnumerable <Class Name> matchs // to match the returned iteration Class Object. However, you can use Var or first define the expected return object type select new {id = student. studentid, name = student. name, age = student. age}; // The page is bound to display GridView1.DataSource = matchs; GridView1.DataBind ();
  • Filtering and sorting: The where statement can be used in the same way as the logical expressions and multiple conditional expressions in SQL syntax, the most special reason is that in the C # source code environment, we can call our own custom method, such as where myfunction (Class Object property value)
// 3 filter and sort IEnumerable <mytestData> matchs; matchs = from student in mydata // student is the alias where student for querying objects in the mydata set. age> 20 // query the filter condition orderby student. age // sort select student; // The data bound to the page displays GridView1.DataSource = matchs; GridView1.DataBind ();
  • Grouping and aggregation: If the returned data is grouped, the returned result is the IEnumerable <T> set of grouping objects. Each group implements the IGrouping <T, k> interface. First, we need to determine the grouping conditions, next, you need to determine what information should be returned for each group.
// 3 grouping and aggregation var matchs = from student in mydata // student is the alias where student for querying objects in the mydata set. age> 20 // query the filter condition orderby student. age // sort group student by student. age into g // g is an iterative IGouping <T, K> object. Each group is an IEnumerable <mytestData> Object select new {age = g. key, avergeage = g. average (student => student. age)}; // display GridView1.DataSource = matchs; GridView1.DataBind ();

Last:

It's been three hours without knowing it. It's too late. I will summarize it today ....

Related Article

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.