9866550012
CSHARP LINQ
Language Integrated Query (LINQ, pronounced "link") is a Microsoft .NET Framework component that adds native data querying capabilities to .NET languages, although ports exist for Java, PHP, JavaScript and ActionScript.
LINQ extends the language by the addition of query expressions, which are akin to SQL statements, and can be used to conveniently extract and process data from arrays, enumerable classes, XML documents, relational databases, and third-party data sources. Other uses, which utilize query expressions as a general framework for readably composing arbitrary computations, include the construction of event handlersor monadic parsers.
LINQ also defines a set of method names (called standard query operators, or standard sequence operators), along with translation rules used by the compiler to translate fluent-style query expressions into expressions using these method names, lambda expressions and anonymous types.
Many of the concepts that LINQ has introduced were originally tested in Microsoft's Cω research project. LINQ was released as a major part of .NET Framework 3.5 on November 19, 2007.
Standard Query Operators
In what follows, the descriptions of the operators are based on the application of working with collections.
The set of query operators defined by LINQ is exposed to the user as the Standard Query Operator (SQO) API. The query operators supported by the API .
- Select
The Select operator performs a projection on the collection to select interesting aspects of the elements. The user supplies an arbitrary function, as a delegate or lambda expression, which projects the data members.
- Where
The Where operator allows the definition of a set of predicate rules that are evaluated for each object in the collection, while objects that do not match the rule are filtered away. The predicate is supplied to the operator as a delegate.
- SelectMany
For a user-provided mapping from collection elements to collections, semantically two steps are performed. First, every element is mapped to its corresponding collection. Second, the result of the first step is flattened by one level. Note: Select and Where are both implementable in terms of SelectMany, as long as singleton and empty collections are available. The translation rules mentioned above still make it mandatory for a LINQ provider to provide the other two operators.
- Sum / Min / Max / Average
These operators optionally take a lambda that retrieves a certain numeric value from each element in the collection and uses it to find the sum, minimum, maximum or average values of all the elements in the collection, respectively. Overloaded versions take no lambda and act as if the identity is given as the lambda.
- Aggregate
A generalized Sum / Min / Max. This operator takes a lambda that specifies how two values are combined to form an intermediate or the final result. Optionally, a starting value can be supplied, enabling the result type of the aggregation to be arbitrary. Furthermore, a finalization function, taking the aggregation result to yet another value, can be supplied.
- Join / GroupJoin
- The Join operator performs an inner join on two collections, based on matching keys for objects in each collection. It takes two functions as delegates, one for each collection, that it executes on each object in the collection to extract the key from the object. It also takes another delegate in which the user specifies which data elements, from the two matched elements, should be used to create the resultant object. The GroupJoin operator performs a group join. Like the Select operator, the results of a join are instantiations of a different class, with all the data members of both the types of the source objects, or a subset of them.
- Take / TakeWhile
- The Take operator selects the first n objects from a collection, while the TakeWhile operator, which takes a predicate, selects those objects that match the predicate.
- Skip / SkipWhile
- The Skip and SkipWhile operators are complements of Take and TakeWhile - they skip the first n objects from a collection, or those objects that match a predicate (for the case of SkipWhile).
- OfType
- The OfType operator is used to select the elements of a certain type.
- Concat
- The Concat operator concatenates two collections.
- OrderBy / ThenBy
- The OrderBy operator is used to specify the primary sort ordering of the elements in a collection according to some key. The default ordering is in ascending order, to reverse the order, the OrderByDescending operator is to be used. ThenBy andThenByDescending specifies subsequent ordering of the elements. The function to extract the key value from the object is specified by the user as a delegate.
- Reverse
- The Reverse operator reverses a collection.
- GroupBy
- The GroupBy operator takes a delegate that extracts a key value and returns a collection of
IGrouping<Key, Values>objects, for each distinct key value. TheIGroupingobjects can then be used to enumerate all the objects for a particular key value. - Distinct
- The Distinct operator removes duplicate instances of a key value from a collection. The function to retrieve the key value is to be supplied as a delegate.
- Union / Intersect / Except
- These operators are used to perform a union, intersection and difference operation on two sequences, respectively.
- SequenceEqual
- The SequenceEqual operator determines whether all elements in two collections are equal and in the same order.
- First / FirstOrDefault / Last / LastOrDefault
- These operators take a predicate. The First operator returns the first element for which the predicate yields true or throws an exception, if nothing matches. The FirstOrDefault operator is like the First operator except that it returns the default value for the element type (usually a null reference) in case nothing matches the predicate. The last operator retrieves the last element to match the predicate, or throws an exception in case nothing matches. The LastOrDefault returns the default element value, if nothing matches.
- Single
- The Single operator takes a predicate and returns the element that matches the predicate. An exception is thrown, if none or more than one element match the predicate.
- ElementAt
- The ElementAt operator retrieves the element at a given index in the collection.
- Any / All / Contains
- The Any operator checks, if there are any elements in the collection matching the predicate. It does not select the element, but returns true for a match. The All operator checks, if all elements match the predicate. The Contains operator checks, if the collection contains a given value.
- Count
- The Count operator counts the number of elements in the given collection.
The Standard Query Operator API also specifies certain operators that convert a collection into another type:
- AsEnumerable: converts the collection to
IEnumerable<T>type. - AsQueryable: converts the collection to
IQueryable<T>type. - ToArray: converts the collection to an array.
- ToList: converts the collection to
IList<T>type. - ToDictionary: converts the collection to
IDictionary<K, T>type, indexed by the key K. - ToLookup: converts the collection to
ILookup<K, T>type, indexed by the key K. - Cast: converts a non-generic
IEnumerablecollection to one ofIEnumerable<T>by casting each element to typeT. Throws an exception for incompatible types. - OfType: converts a non-generic
IEnumerablecollection to one ofIEnumerable<T>. Only elements of typeTare included.
No comments:
Post a Comment