C # Introduction to LINQ Technology

Source: Internet
Author: User

LINQ technology: solves the problem of reducing the complexity of access and integration of information data, that is, the collection access technology.

Language-level INtegrated Query)

C #3.0 query syntax

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:

Class Program

{

Static void Main (string [] args)

{

Int [] arr = new int [] {8, 5, 89, 3, 56, 4, 1, 58 };

Var m = from n in arr where n <5 order by n select n;

Foreach (var n in m)

{

Console. WriteLine (n );

}

Console. ReadLine ();

}

}

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.

Q: Why does the syntax of a linq query start with the from keyword instead of the select keyword? The statement at the beginning of select is more similar to the SQL statement and easier to understand?

A: To put it simply, for the intelligent perception (Intelisence) function of IDE, the select keyword is put behind, and the VB9 era is always put ahead.

The following is an explanation and supplement of the LINQ syntax design (the form process of the linq syntax). You can try to change it to the form of the LINQ syntax:

I feel that C # designers have poor consideration for the syntax of LINQ. I am not very familiar with LINQ, but I will give an example to illustrate my consideration for the syntax Design of LINQ:

Problem: select the range from int array a to 18 ~ The value between 28 and the int array B is output in ascending order.

Known: int [];

Solution: int [] B = SQL {select int from a where int> = 18 and int <= 28 order by int };

Problem: select Point list B which is no more than 100 away from Point O from struct Point list.

Known: List <Point> a; Point O; function float Distance (Point A, Point B );

Solution: List <Point> B = SQL {select Point from a where Distance (Point, O) <= 100 };

Problem: Set all numbers in string s to '0'

Known: string s; char [] chars = s. ToCharArray ();

Solution: char [] result = SQL {update chars set char = '0' where char> = '0' and char <= '9 '};

Problem: insert 255 After byte array a to form a new array B

Known: byte [];

Solution: byte [] B = SQL {insert into a values (255 )};

Problem: Delete the negative value in int array a to form a new array B.

Known: int [];

Solution: int [] B = SQL {Delete int from a where int <0 };

The difference between LINQ and SQL: although the two are similar in usage, they are in completely different language environments. LINQ processes object objects, while SQL processes tables. For example:

Int [] A = {...};
// Search for non-negative integers from the array and sort them in ascending order
From n in a where n> = 0 order by N select n

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.