LINQ Element Operators (FirstOrDefault, Last, ElementAt, SingleOrDefault)

  Total Views : 173
  Comments : 0
 
  Keywords : Element operators in linq with examples in c# / vb.net, Different type of linq element operators first / last / elementat / single / defaultifempty in c# / vb.net with examples.

Here we will learn LINQ element operators in c#, vb.net with examples and different type of element operators (first, firstordefault, last, lastordefault, elementat, elementatordefault, single, singleordefault and defaultifempty) with examples.

LINQ Element Operators

In LINQ element operators are used to return a first or last element of list or single element from collection or a specific element based on index value from a collection. 

 

In LINQ we have different type of element operators available those are

 

  1. First
  2. FirstOrDefault
  3. Last
  4. LastOrDefault
  5. ElementAt
  6. ElementAtOrDefault
  7. Single
  8. SingleOrDefault
  9. DefaultIfEmpty

By using these element operators we can get list / collection items at specific position. Following table shows more detailed information related to element operators.

 

OperatorDescription
First It returns first element in sequence or first element from the collection based on specified condition
FirstOrDefault Its same as First but it returns default value in case if no element found in collection
Last It returns last elements in sequence or last element from the list based on matching criteria
LastOrDefault Its same as Last but it returns default value in case if no element found in collection
ElementAt It returns an element from the list based on specified index position
ElementAtOrDefault Its same as ElementAt but it returns default value in case if no element present at specified index of collection
Single It returns single specific element from the collection
SingleOrDefault Its same as Single but it returns default value in case if no element found in collection
DefaultIfEmpty It returns default value in case if the list or collection contains empty or null values

In further chapters we will learn all the element operators in detail with examples.

 

We will be studying each of them with a demo so that we can get a clear understating on the operators. So let’s start with a demo.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User