C # What does 3.0 bring to us (1) -- Lambda of LINQ

Source: Internet
Author: User
I haven't followed up on new technologies in the past year, and people are getting old.
Today, I got the beta2 version of vs2008, and I learned that the official version has long been available when I checked the materials for a while. Actually made a Martian.
It was only visible when we found out that LINQ. Int [] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
VaR lownums = from num in numbers
Where num <5
Select num;

The SQL-like set operation method has to be excited. This is called Lambda in C #3.0.
In addition, there is another way of writing:

VaR lownums = numbers. Where (I => I <5). Select (I => I );

Let's take a look at query keywords.
From: variable definition, but it is not very reliable with the word from.
The first I EXPRESSION AFTER THE WHERE clause in the second statement is also this concept.

Where: Query condition, which is the same as SQL.
The second method is the part after "=>.

Select: return value, which is generally a variable defined by from and can be computed. For example, select num + 1.
Group: Group.

String [] words = {"blueberry", "chimpanzee", "abacus", "banana", "apple", "Cheese "};
VaR wordgroups =
From W in words
Group W by W [0];

In this Code, wordgroups is the query result, a set of ienumerable consisting of igrouping. The first igrouping set of ienumerable contains "blueberry" and "banana ".
VaR wordgroups = words. groupby (I => I [0]);

Into: insert, similar to creating a temporary table in SQL.
View code

VaR wordgroups1 =
From W in words
Group W by W [0] into newgroup
Where newgroup. Count ()> = 2
Select New {firstletter = newgroup. Key, words = newgroup. Count ()};

This sentence means that words in words are grouped by the first letter (the same as the Demo code of the group keyword) and placed into newgroup, which is a set of ienumerable, then, Filter Based on the Count attribute of newgroup. The filter set is stored in a set composed of new objects. This object has two attributes: firstletter and words. Naturally, the source of these two attribute values is the key and count attributes of newgroup.
Orderby: Sort

Ienumerable <string> sortedwords =
From W in words
Orderby W ascending // or descending
Select W;

Join: Like a foreign key association in a database. VaR query =
From C in categories
Join P in products on C equals P. Category
Select New {Category = C, p. name };

In fact, this statement returns that the category attribute value in products has an object in the categories dictionary set, and then assigns the set of the new object consisting of the categpry and name attributes of the object to the query.
Let: store temporary values.

VaR query =
From sentence in strings
Let words = sentence. Split ('')
From word in words
Let W = word. tolower ()
Where W [0] = 'A' | W [0] = 'E'
| W [0] = 'I' | W [0] = 'O'
| W [0] = 'U'
Select word;

This is like two nested for loops, similar to the implementation of the following code.

List <string> resualt = new list <string> ();
Foreach (string sentence in strings)
{
String [] words = sentence. Split ('');
Foreach (string word in words)
{
If (word. tolower () = 'A' | word. tolower () = 'E' | word. tolower () = 'I'
| Word. tolower () = 'O' | word. tolower () = 'U ')
{
Resualt. Add (Word );
}
}
}

Come here first. It's too difficult to get started at a.m., and the second writing method is still incomplete. Change to another day.

(If you find any mistakes, please correct them .)
C # What does 3.0 bring to us (1) -- Lambda of LINQ
C # What does 3.0 bring to us (2) -- Automatic attributes
C # What does 3.0 bring to us (3) -- initializer
C # What does 3.0 bring to us (4) -- var, a local variable with implicit type
C # What does 3.0 bring to us (5) -- anonymous type
C # What does 3.0 bring to us (6) -- Expansion Method

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.