Common Methods in LINQ

Source: Internet
Author: User

These days idle is also idle, on the careful study of LINQ syntax, and some of his extension methods of use.

The following are some common extension methods.

Aggregate

Custom Aggregation Calculations

All

Detects whether all elements in a sequence meet the specified criteria

Any

Detects if there are elements in the sequence that meet the specified criteria

Average

Calculates the average of elements in a sequence

Cast

Converts the type of an element in a sequence to the specified type ( specified by the TResult parameter )

Contact

Appends all elements of a sequence to another sequence and forms a new sequence.

Contains

Detects whether the specified element exists in the sequence.

Count

Calculates the number of elements in a sequence, or the number of conditional elements that are satisfied in a sequence.

DefaultIfEmpty

Returns a sequence of type ienumerable<t>. Returns a sequence that contains only one element (the default value or a specified value ) If the sequence is empty

Distinct

You can remove duplicate elements from the data source and return a new sequence. In addition, he can specify a comparer to compare whether two elements are the same.

Element

Returns the element at the specified index in the collection.

Elementatordefault

Returns the element at the specified index in the collection. If the index exceeds the range of the collection, the default value is returned

Empty

Returns an empty sequence of type ienumerable<t>

Equalall/sequenceequal

Determines whether two sequences are equal.

Except

You can calculate the difference set of two sets (a collection of elements that are in one collection and not in another collection )

First

Returns the first element of the collection, or returns the first element in the collection that satisfies the setting condition.

FirstOrDefault

Returns the first element of the collection, or returns the first element in the collection that satisfies the setting condition. If there are no elements that satisfy the condition, the default element is returned.

GroupBy

Group elements in a sequence

GroupJoin

He produces a hierarchical data structure that reduces each element in a collection to match a set of related elements in the second set. In the query results, the elements in the first collection appear in the query results. If the element in the first collection finds the related element in the second collection, the element that is found is used, otherwise an empty

Intersect

You can calculate the intersection of two sets (the element is in the first sequence, also in the second sequence )

Join

Requires that the connection relationship of the element must satisfy the two data sources that are linked , similar to the inner join sentence in the SQL statement

Last

Returns the last element of the collection, or returns the last element of the collection that satisfies the specified condition.

LastOrDefault

Returns the last element of the collection, or returns the last element of the collection that satisfies the specified condition. If the element that satisfies the condition exists, the default element is returned.

LongCount

Calculates the number of elements in a sequence, or computes the number of elements that the sequence satisfies. Half calculates the number of elements in a large collection.

Max

Calculates the maximum value of an element in a sequence.

Min

Calculates the minimum value of an element in a sequence.

OfType

Filters the elements of a specified type from a sequence and makes up a sequence.

By

The ascending order of the elements in the sequence according to the keyword.

OrderByDescending

The descending order of the elements in the sequence based on the keyword.

Range

Returns a sequence of numbers for the specified range.

Repeat

Returns the ienumerable<t> type that contains a repeating worth sequence.

Reverse

Reverses the order of the elements in the sequence.

Select

Projects elements in the data source into a new sequence and specifies the type and representation of the element.

SelectMany

You can project elements in a data source into a new sequence and develop the type and expression of the element.

Single

Returns the unique element of the collection, or returns the unique element of the collection that satisfies the specified condition.

Singleordefault

Returns the unique element of the collection, or returns the unique element of the collection that satisfies the specified condition. If there are no elements that satisfy the condition, the default element is returned.

Skip

Skips the specified number of elements in the data source (sequence) , and then returns the remaining elements or sequences.

SkipWhile

Skips elements in the data source ( sequence ) that meet the specified criteria, and then returns the remaining elements or sequences.

Sum

Computes the and of the elements in the sequence.

Take

Extracts the specified number of elements from the beginning of the data source (sequence). It then returns a sequence that consists of these elements.

TakeWhile

Extracts the element that satisfies the specified condition starting at the beginning of the data source (sequence). It then returns a sequence that consists of these elements.

ThenBy

Sorts the elements in the sequence in ascending order based on the secondary key.

Therebydescending

The elements in the sequence are sorted in descending order based on the secondary keyword.

ToArray

converts a sequence of type ienumerable<t> to an array of type t[].

ToDictionary

The elements in the sequence are placed in a one-to-one dictionary sequence (dictionary<tkey,tvalue>) by key values .

ToList

converts a sequence of type ienumerable<t> to a sequence of type list<t>.

ToLookup

The elements in the sequence are placed in a one-to-many dictionary sequence (lookup<tkey,tvalue>) by key values .

Union

You can calculate the set of two sets (a collection that consists of elements in one collection, or in another collection )

Where

Handles logical expressions consisting of logical operators, such as logical and , logical, or, and filters data from the data source.

These methods can be broadly divided into several categories:

Projection Operations

Select

  SelectMany,

Link Action

Join

GroupJoin

Filtering actions

Where

Sort operations

By

OrderByDescending

thereby

Therebydescending

Reverse

Aggregation operations

Max

Min

Count

LongCount

Sum

Aggregate

Collection operations

Distinct

Except

Intersect

Union

Element manipulation

Element

Elementatordefault

First

FirstOrDefault

Last

LastOrDefault

Single

Singleordefault

Data type conversion operations

AsQueryable: You can convert a data source into a collection sequence of iqueryable<t> or iiiiiqueryable.

AsEnumerable: You can convert a data source into a collection sequence of ienumerable<t> or IEnumerable.

Cast: Converts the type of the element in the sequence to the specified type.

OfType: Filters an element of a specified type from a sequence and forms a sequence.

ToArray: Converts a ienumerable<t> sequence into an array of type t[].

ToList: Converts a sequence of ienumerable<t> into a sequence of one list<t> type.

ToDictionary: The elements in the sequence are placed in a one-to-one dictionary sequence by key value.

ToLookup: The elements in the sequence are placed in a one-to-many dictionary sequence by key value.

Qualifier action

All

Any

Contains

And also

Skip

SkipWhile

Take

TakeWhile

Contact

SequenceEqual

Common Methods in LINQ

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.