. Net things (5) -- C #3.0 LINQ query syntax

Source: Internet
Author: User

I. Introduction to LINQ

LINQ, Language-level integrated query (LAnguageInTegratedQUery)

Over the past 20 years, the application of object-oriented programming technology (OO) in the industrial field has entered a stable development stage. Programmers now recognize language features such as classes, objects, and methods. Observing the present and next-generation technologies, a major challenge of new programming technologies has begun to emerge. That is, since the birth of object-oriented technology, it has not solved the problem of reducing access and integrating information (accessing and integrating information) complexity issues. Two of the most accessed data sources are related to databases and XML.

A more general approach is provided.. NET Framework adds query facilities that can be applied to all sources of information ), this is a better way than adding relational or XML-like features to the development language and runtime. These syntax features are called. Net Language Integrated Query (LINQ ).

Ii. LINQ syntax

1. How to read the content of LINQ: (1) Lin K (2) Lin Q
2. Key words of LINQ: From, select, in, where, group by, order...
3. Note: it must end with select or group.
4. the semantics of LINQ:
From temporary variable in collection object or database object
Where condition expression
[Order by condition]
Value queried in the select Temporary Variable
[Group by condition]

III. C #3.0 LINQ example

First, let's look at a very simple LINQ query example to query numbers smaller than 5 in an int array and sort them in order of size:

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace linq1 <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> int [] arr = new int [] {8, 5, 89, 3, 56, 4, 1, 58 }; <br/> var M = from N in arr where n <5 orderby n select N; <br/> foreach (var n in m) <br/> {<br/> console. writeline (n); <br/>}< br/> console. readline (); <br/>}< br/>

The above Code except the LINQ query syntax is the syntax we are familiar with, while the LINQ query syntax is familiar with the SQL query syntax, except for the order.

Let's look at a relatively complex example: in the language strings we listed, we want to list them by character length and classification. The implementation code is as follows:

Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. text; </P> <p> namespace linq1 <br/>{< br/> class Program <br/>{< br/> static void main (string [] ARGs) <br/> {<br/> string [] ages = {"Java", "C #", "C ++", "Delphi", "VB.net ", "vc.net", "C ++ builder", "kylix", "Perl", "Python "}; <br/> var query = from item in ages <br/> orderby item <br/> group item by item. length into lengthgroups <br/> orderby lengthgroups. key descending <br/> select lengthgroups; <br/> foreach (VAR item in query) <br/>{< br/> console. writeline ("strings of length", item. key); <br/> foreach (var val in item) <br/>{< br/> console. writeline (VAL); <br/>}< br/> console. readline (); <br/>}< br/>

References

1. C #3.0 LINQ query syntax: http://blog.joycode.com/ghj/archive/2007/06/07/103960.aspx

2. Special Topics on LINQ: http://tech.it168.com/zt/linq/index.html

3, XML and LINQ practice Details: http://www.maeee.cn/article/it/linq/show/2008122211350135.shtml

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.