Part 20 Element Operators in LINQ











############################# Video Source: www.youtube.com/watch?v=oC1z7BP_yoE

Text version of the video • http://csharp-video-tutorials.blogspo... • Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help. •    / @aarvikitchen5572   • Dot Net, SQL, Angular, JavaScript, jQuery and Bootstrap complete courses • https://www.youtube.com/user/kudvenka... • The following standard query operators belong to Element Operators category • First / FirstOrDefault • Last / LastOrDefault • ElementAt / ElementAtOrDefault • Single / SingleOrDefault • DefaultIfEmpty • Element Operators retrieve a single element from a sequence using the element index or based on a condition. All of these methods have a corresponding overloaded version that accepts a predicate. • First : There are 2 overloaded versions of this method. The first overloaded version that does not have any parameters simply returns the first element of a sequence. • If the sequence does not contain any elements, then First() method throws an InvalidOperationException. • The second overloaded version is used to find the first element in a sequence based on a condition. If the sequence does not contain any elements or if no element in the sequence satisfies the condition then an InvalidOperationException is thrown. • FirstOrDefault : This is very similar to First, except that this method does not throw an exception when there are no elements in the sequence or when no element satisfies the condition specified by the predicate. Instead, a default value of the type that is expected is returned. For reference types the default is NULL and for value types the default depends on the actual type expected. • Last : Very similar to First, except it returns the last element of the sequence. • LastOrDefault : Very similar to FirstOrDefault, except it returns the last element of the sequence. • ElementAt : Returns an element at a specified index. If the sequence is empty or if the provided index value is out of range, then an ArgumentOutOfRangeException is thrown. • ElementAtOrDefault : Similar to ElementAt except that this method does not throw an exception, if the sequence is empty or if the provided index value is out of range. Instead, a default value of the type that is expected is returned. • Single : There are 2 overloaded versions of this method. The first overloaded version that does not have any parameters returns the only element of the sequence. • Single() method throws an exception if the sequence is empty or has more than one element. • The second overloaded version of the Single() method is used to find the only element in a sequence that satisfies a given condition. An exception will be thrown if any of the following is true • a) If the sequence does not contain any elements OR • b) If no element in the sequence satisfies the condition OR • c) If more than one element in the sequence satisfies the condition • SingleOrDefault : Very similar to Single(), except this method does not throw an exception when the sequence is empty or when no element in the sequence satisfies the given condition. Just like Single(), this method will still throw an exception, if more than one element in the sequence satisfies the given condition. • DefaultIfEmpty : If the sequence on which this method is called is not empty, then the values of the original sequence are returned. • If the sequence is empty, then DefaultIfEmpty() returns a sequence with the defualt value of the expected type. • The other overloaded version with a parameter allows us to specify a default value. If this method is called on a sequence that is not empty, then the values of the original sequence are returned. If the sequence is empty, then this method returns a sequence with the specified defualt value.

#############################









Content Report
Youtor.org / Youtor.org Torrents YT video Downloader © 2024

created by www.mixer.tube