LinQ query and linq dynamic query

Source: Internet
Author: User

LinQ query and linq dynamic query

A lot of new content has been added to the project, and LINQ is a very simple but practical content, making it easier for us to operate data. Next, let's give a brief introduction to LINQ.


I. Overview

LinQ (Language-Integrated Query, Language integration Query)It can provide powerful query functions for C # and VB. LinQ introduces a standard and easy-to-learn Data Query and update mode, which can be expanded to support almost any type of data storage. VS contains the assembly of the LINQ provider, which supports. NET Framework set, SQL sever database, ADO.. NET data collection XML documents are used together, thus setting up a bridge between the object field and the data field, solving the problem of long-term split between the object field and the data access field and independent governance.


LINQ mainly includes four key technologies: LINQ to SQL, LINQ to DataSet, LINQ to Object, and LINQ to XML.


Ii. Advantages

Using LINQ has many advantages, solving the disadvantages of poor SQL coding experience and solving problems more conveniently and efficiently.

1. Get started without complex learning processes

2. Write less code to create a complete application.

3. faster development of applications with fewer errors.

4. data sources can be merged without the help of programming skills.

5. Higher development efficiency for new developers.


Iii. Practical Application

I have a simple understanding of LINQ. Let's use several examples to describe its main application methods.

1. Basic Methods: selection, sorting, and grouping

First, declare a set:

// List of person generic sets <string> person = new List <string> (); person. add ("zhang san"); person. add ("zhang sisi"); person. add ("li si"); person. add ("wang wu"); person. add ("wang huanhuan"); person. add ("li hong"); person. add ("xu jinglei"); person. add ("zhang xinyu ");

Basic method:

//// 1. Output all elements var result = person. select (p => p); // 2. The output starts with 2. var result = person. where (p => p. startsWith ("z"); var result = person. select (p => p ). where (p => p. startsWith ("z"); var result = person. where (p => p. startsWith ("z ")). select (p => p); // 3. Sort // var result = person. orderBy (p => p); // sort by the last letter of the name var result = person. orderBy (p => p. substring (p. length-1, 1 )). select (p => p); // 4. Group by name -- retrieve the part before the space in the name var result = person. groupBy (p => p. split (new char [] {''}) [0]); foreach (var group in result) {Console. writeLine ("surname:" + group. key); foreach (var name in group) {Console. writeLine ("\ t" + name);} Console. writeLine ();}}

2. Advanced Query Method

· Aggregation class: count, Max/Min, Average

· Sorting class: thenBy

· Partition class: Take, TakeWhile, Skip, and SkipWhile

· Collection class: Distinct

· Generation class: Range and Repeat

Int [] arr = {343, 45, 45, 67, 6, 7,453, 34, 76,456 4, 345, 3, 5 }; // advanced method // 1. Aggregation Console. writeLine ("maximum value of arr" + arr. max (); // 4546 Console. writeLine ("minimum value of arr" + arr. min (); // 3 Console. writeLine ("average value of arr" + arr. average (); // Average Console. writeLine ("arr and" + arr. sum (); // Sum Console. writeLine ("Number of arr" + arr. count (); // number // 2. Sorting class var result = arr. orderBy (p => p. toString (). substring (0, 1); var result = arr. orderBy (p => p. toString (). substring (0, 1 )). thenBy (p => p); // secondary sorting // partition class var result = arr. skip (3 ). take (3); // skip three values and Take three values var result = arr. skipWhile (p => p> 7); // 6, 7,453, 34, 76,456 4, 345, 3, 5 var result = arr. takeWhile (p => p> 7); // 343, 45, 45, 67 var result = System. linq. enumerable. range (); // generate continuous sequence -- generate class var result = System. linq. enumerable. repeat (10, 50); // generate a recurring series-generate a class







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.