Use LinQ to query data objects, and use linq to implement data objects.

Source: Internet
Author: User

Use LinQ to query data objects, and use linq to implement data objects.

As a result of project requirements, I have come into contact with LinQ. After some time of study, I have a simple understanding of LinQ. This article will briefly describe this topic.

First of all, we should write several questions to understand from a macro perspective. What is LinQ? Why use LinQ? What are the benefits of using it?

Language Intergrated Query is a group of extensions used for C # and vb. It allows you to write C # Or vb code to Query memory data in the same way as a database. ---- Baidu encyclopedia

In layman's terms, data is queried. So why use it to query data?

We will consider this issue from a slightly abstract perspective. Object-Oriented Programming and data access separation: an object-oriented language and database have two completely unrelated data types. The string in the programming language is varchar in the database, there is no uniformity at all. Second, the SQL coding experience lags behind. We write SQL statements without prompting the statements. Only after the statements are completed can we know whether they are correct and the customer experience is poor; the three query languages are inconsistent. XML and Databases both have their own data query systems, and objects do not have their own Query Systems. Based on the above problems, the emergence of LinQ.

Composition as mentioned above, LinQ includes three types of content queries, objects, XML files and databases. Database queries are divided into three parts:

Linq to SQL,

Linq to DataSet,

.


Query Method: You can query data in two ways: Language Query and method query. A query statement is similar to an SQL statement. This method is readable but has many restrictions, making it difficult to implement multi-condition queries. Query Method: This method is not readable, but can meet many complex query requirements, because we can implement a separate method for query conditions. Generally, to make the code readable, we should first consider using the query language method. If the query language is difficult to implement, we should combine the query language and the query method for mixed compilation, this maximizes code readability and enhances the query function.

The following advanced query methods are available:

Aggregation: Max, Min, Count, Sum, etc;

Sorting: ThenBy, OrderBy, etc;

Partitions: Take, Skip, TakeWhile, and SkipWhile;

Set: Distinct

Generation: Range, Repeat

 

The following are examples of a few LinQ queries:

1. Language-and method-based mixed-encoding query (linq to objects ):

<Span style = "font-size: 18px;"> private void btnQuery_Click (object sender, EventArgs e) {// basic method of LinQ To Object // List of person data in a generic set <String> person = new List <String> (); person. add ("Old Big"); person. add ("Old Two"); person. add ("Old Three"); person. add ("Old four"); person. add ("Old Five"); person. add ("sat"); person. add ("Xiao Qi"); person. add (""); // output all elements of person // var result = from p in person select p; // output all the people whose names start with "person" // mixed statement and method compiling mode. Use the statement query method to filter var result = (from p in person select p ). where (p => p. startsWith ("old"); // print result. print ();} public static void Print (this IEnumerable <String> ie) {// obtain the traversal interface IEnumerator <String> result = ie. getEnumerator (); while (result. moveNext () {Console. writeLine ("\ n ---------------------- \ n"); Console. writeLine (result. current); Console. writeLine ("\ n ---------------------- \ n") ;}</span>

This code first defines a generic set object person, and then uses the query statement and the query Fei ang FA mixed encoding mode to query people starting with "old" and print it out. The query statement cannot complete all the query functions. You need to use the query method to filter them. The query method in this example is provided by the system. It can be seen that the query statement is readable, but not powerful enough. The query method is not readable and has many functions.

<Span style = "font-size: 18px;"> 2. Based on the query method mode (linq to objects) private void btnSelect_Click (object sender, EventArgs e) {// basic method of LinQ To Object // List of person data in a generic set <String> person = new List <String> (); person. add ("Old Big"); person. add ("Old Two"); person. add ("Old Three"); person. add ("Old four"); person. add ("Old Five"); person. add ("sat"); person. add ("Xiao Qi"); person. add (""); // Method 4 call the external complex method as the query condition var result = person. where (p => Judge (p);} public bool Judge (String s) {if (s. startsWith ("old") {return true;} else {return false ;}</span>
The functions of this Code are the same as those in the previous example. The query method is used to complete the filtering function. This method is different from the method in the previous example.
<Span style = "font-size: 18px;"> 3. Database-Based Query (LinQ to Entities) public ActionResult Index () {// 1 use the linq statement to obtain object data // The lambda expression List <Models. blogArticle> list = (from B in db. blogArticle where B. AIsDel = false select B ). toList (); // 2 Use viewdata to obtain the data ViewData ["DataList"] = list; // 3 load the View list. print ();} public partial class BlogArticle {entity code: omitted} </span>
<span style="background-color: rgb(255, 255, 255);">       </span><span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif;"> </span>

In this example, we only replace the data object with an object set with the same basic syntax.

Advantage: easy to use. It is more intuitive to query data in the form of objects and use them in combination with query statements and query methods.

Disadvantage: using the delayed Loading Function, linq puts the data to be operated into the memory, occupying a large amount of resources, resulting in a waste of resources.


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.