LINQ Extension Methods
● ThenBy: ○ Destructive Sort: The regular “OrderBy” method cannot be dotted together with itself; you cannot “OrderBy(firstName). OrderBy(lastName)” to chain two OrderBy operations together as the second OrderBy operation will overwrite the first. ○ OrderBy(). ThenBy(): The solution is to use “ThenBy” which is a non-destructive secondary sorting operation. ○ . ThenBy(). ThenBy(). ThenBy()...: As many ThenBy() operations can be chained together as required since it is non-destructive. ● OfType: ○ Strongly Types a collection: Takes an IEnumerable in and returns a strongly typed Enumerable: ■ Collection refers to a collection which implements IEnumerable but is not strongly Typed. ■ SpecifiedType refers to the desired Type which the IEnumerable should be. ■ This IS a Sorting operation: If anything doesn’t fit our SpecifiedType, it won’t be included in the result returned by OfType. ● Finding one element out of a Collection: ○ SingleOrDefault: Tries to find a matching item in the collection or Enumeration we are looking at based on some “predicate” (boolean evaluation). ■ If it finds it: If SingleOrDefault finds a matching item, it will return that item. ■ If it doesn’t find it: That’s what “OrDefault” is for; it returns the default value for a given type. ● For Reference Type Variables: “OrDefault” returns Null. ● For Value Type Variables: What “OrDefault” returns depends on the Value type. For “int” it returns “0”. ○ If it finds more than one item which matches: Throws an exception; “Single” only wants to find 1 item which matches, so it is an error for it to find anything beyond this. ○ Single: Similar to SingleOrDefault, but if it finds more than one OR none it throws an exception ○ First: Similar to SingleOrDefault; if it finds none it throws an exception, otherwise it will return the First occurrence it finds, no matter how many matches it finds. ○ FirstOrDefault: Similar to SingleOrDefault; but it throws no exceptions; if it finds none it returns null. ○ Last / LastOrDefault: Behaves like First and FirstOrDefault respectively. ● Deciphering The “Join” Method Signature: https: //www. youtube. com/watch? v=tzR2qY6S4yw ○ Join(): Joins 2 sets of Data together. The means by which this is done is most easily illustrated through example. For both the Query and Fluent Syntax examples below, the following data will be used: ■ A list of people: The variable “people” will represent a list of type “People”. Each “People” instance will have an “ID” integer property as well as a “Name” string property.
■ A list of orders: The variable “orders” will represent a list of type “Order”. Each “Order” instance will have an “orderID” integer property as well as a “OrderDate” Date property. ○ Query Syntax: ■ Join in, on: The join clause handles the bulk of the work in this statement, creating “o” as well as handling the predicate for which the two collections will be joined, in this case, that p. ID equates with o. orderID ■ New {Name / OrderDate}: The select clause creates a dynamic variable which is stored in “orderDates”. This is the reason we need to use the “var” keyword; “orderDates” is not strongly typed. ○ Fluent Syntax: The method signature for Join seems quite daunting since it features 4 Generic Types and 5 Parameters, 3 of which are delegates: Public static IEnumerable< TResult> Join< TOuter, TInner, TKey, TResult> ( ■ TResult: The expected return type for the join method ■ TOuter: One of the 2 types which will be joined. Since this behaves like an Extension Method, TOuter can implicitly be set to the Type of the collection upon which the join method is called. In our example, this was “ people ” ■ TInner: The Type of the second Enumerable which will be joined. In our example, this was “ orders ” ■ TKey: A type which implements the IEquatable interface, (whether two objects are equivalent to each-other) ■ Func< TOuter, TKey>: One half of the predicate on which the two collections will be joined, in our example, this was “ p. ID ” ■ Func< TInner, TKey>: The other half of the predicate on which the two collections will be joined, in our example, this was “ o. orderID ” ■ Func< TOuter, TInner, TResult>: Outer and Inner simply refer to “Person” and “Order” respectively, TResult is our return type. This function performs the same data transformation as the “select” clause, which in our example was “ new {p. Name, o. OrderDate} ” ■ All together now: Below is the same statement from the Query Syntax now written using Fluent Syntax: ○ Additional Sorting Criteria: To add additional data sorting such as “Where” or “OrderBy” the syntax differs based on whether you are using Query or Fluent. ■ Query: Additional clauses are added following the join clause: ■ Fluent: Adding the same “Where” and “OrderBy” methods to our existing syntax can be accomplished by dotting the methods together:
● RemoveAll: This method can be used to remove multiple elements from a list wich match a certain predicate: ○ MyList. RemoveAll(t => t. Name == " ABC" || t. Name == " XYZ" ); ■ This example calls “RemoveAll” on “MyList” removing any element whose “Name” property matches the string “ABC” OR “XYZ” ○ Another approach: List< T> toRemove =... MyList. RemoveAll(t => toRemove. Contains(t. Name)); ■ Specifies a list of elements to remove
Воспользуйтесь поиском по сайту: ©2015 - 2024 megalektsii.ru Все авторские права принадлежат авторам лекционных материалов. Обратная связь с нами...
|