Projection, filtering, and sorting of LINQ learning in C #

Source: Internet
Author: User

Select

A select statement is used to project a set sequence based on a given condition. A select statement can return the filtering results of a combination, return the anonymous type, operate on the returned results, and return the combined subquery results.
The select method is defined as follows:
Public static ienumerable <tresult> select <tsource, tresult> (this ienumerable <tsource> source, func <tsource, tresult> selector)

This extension method is defined in the enumerable type.

// Data source type attributes
VaR result = from student in datasource. Students
Where student. Name. length> 3
Select student. Name;

// Result of data source type filtering
VaR result = from student in datasource. Students
Where student. Name. length> 3
Select student;

// New Type
VaR result = from student in datasource. Students
Where student. Name. length> 3
Select New Student {name = student. Name, studentid = student. studentid };

// Anonymous type
VaR result = from student in datasource. Students
Where student. Name. length> 3
Select New {name = student. Name, studentid = student. studentid };

// Perform operations on the returned results
VaR result = from student in datasource. Students
Where student. Name. length> 3
Select student. tostring ();

The returned result is ienumerable <t> type.

Selectworkflow

Selectworkflow is defined as follows:
Public static ienumerable <tresult> selectult <tsource, tresult> (this ienumerable <tsource> source, func <tsource, ienumerable <tresult> selector)

The prototype shows that the ienumerable <t> interface should be implemented for each element type of the filtering result.
String implements the ienumerable <t> interface, which can be used to construct a scenario: Query all character sequences that comprise the Student name.
VaR result = datasource. Students. selectmany (STR => Str. Name );

The equivalent select clause is:
VaR result = from student in datasource. Students
From Ch in student. Name
Select ch;

It can be considered that selectsequence projects each element of the sequence to ienumerable <(of <(T>)> and merges the result sequence into a sequence.

Distinct

Prototype:
Public static ienumerable <tsource> distinct <tsource> (this ienumerable <tsource> source)
Remove duplicate elements from the projection result set. This operation can only be performed by calling methods.
In the same scenario: query the non-Repeated sequences of all characters that constitute the Student name.
VaR result = datasource. Students. selectiterator (STR => Str. Name). Distinct ();

First, last, Skip, take, single

First select the first element of the set.
VaR result = datasource. Students. Select (student => Student). First (); // student ID: 1, Student name: Andy
The last element of the last selection set.
VaR result = datasource. Students. Select (student => Student). Last (); // student ID: 4, Student name: dark
Skip skips n elements.
VaR result = datasource. Students. Select (student => Student). Skip (2). Count (); // 2
Take: select the first n elements of the set.
VaR result = datasource. Students. Select (student => Student). Skip (2). Take (1). Count (); // 1
Single returns the unique element of the sequence. If the sequence does not exactly contain one element, an exception is thrown.
VaR result = datasource. Students. Select (student => Student). Single (); // exception: sequence contains more than one element

Orderby [Descending]

Sort the set.
Public static iorderedenumerable <tsource> orderby [Descending] <tsource, tkey> (this ienumerable <tsource> source, func <tsource, tkey> keyselector [, icomparer <tkey> comparer])

Tkey must have implemented the icomparer <t> interface.

VaR result = from student in datasource. Students
Orderby student. Name descending
Select student. Name;
// Equivalent:
VaR result = datasource. Students. orderbydescending (student => Student. Name). Select (student => Student. Name );
// Result:
// Dark
// Cindy
// Bill
// Andy

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.