Web
Analytics
Element Operators in LINQ C# - Tutorial

For Consultation : +91 9887575540

Stay Connected :

Element Operators in LINQ C#

Element operators return single value or a default value from input sequence.In this blog I am demonstrating following linq element operators.

  • first
  • firstordefault
  • last
  • lastordefault
  • single
  • singleordefault
  • elementat
  • elementatordefault
  • defaultifempty

1. First 

It returns first from a list, array and  collection etc. 

Example

 

 

Note : We can also get first elements using predicate function or lambda expression, but remember whenever any elements does not find as per lambda express , first operator throws an exception(“System.InvalidOperationException : Sequence contains no matching element.” ) .In this condition we will use firstordefault operator. 

Now we will see that how we can get element using lambda expression.

Example

 

OutPut

First

 

FirstOrDefault element operator in linq C# 

It works same as first element operator but the difference between first and firstordefault is that firstordefault returns default value of element rather than throw exception which is thrown by first element operator.

 

firstordefault_linq_Csharp

Last and LastorDefault in LINQ C#

3. Last: return last element, if predicate is defined and no elements are find as per prdicate condition then it will throw exception.

4. LastorDefault: It works same as Last operator but it will return input list element’s default value rather than exception.

 

last_and_lastordefault_linq
Single and SingleorDefault 

5. Single

The Single query operator returns the only element in the input sequence. If the input sequence contains more than one element, an exception is thrown: ”System.InvalidOperationException : Sequence contains more than one element.” If the input sequence contains zero elements, an exception will also be thrown: “System.InvalidOperationException : Sequence contains no elements.”

6. SingleorDefault:

It is very much similar as Single but the only difference is If the predicate version is being used and no matching elements are found, the default value is again returned, rather than an exception

 

Single_and_SingleorDefault_linq
ElementAt and ElementAtDefault in LINQ C#

6. The ElementAt query operator returns the element that exists at the specified position in the input sequence.

Notice that the value passed to ElementAt is a zero-based index; for example, to select the first element, the value would be 0.

If the value passed to ElementAt tries to select an element that is greater than the number of elements in the input sequence, an exception will be thrown: “Index was out of range. Must be non-negative and less than the size of the collection.”

7. The ElementAtOrDefault query operator works in a similar way to ElementAt, but rather than throw an exception if the value passed to it exceeds the size of the input sequence, it will return the default value for the input type.

ElementAt_ElementAtDefault_LINQ

8. DefaultIfEmpty 

The DefaultIfEmpty query operator takes an input sequence and does one of two things. If the input sequence contains at least one element, then the input sequence is returned without any changes. If, however, the input sequence contains zero elements, the output sequence returned will not be empty; it will contain a single element with a default value.

 

DefaultifEmpty_linq

All Source Code can be downloaded from link given below

Download Source Code

Select your currency