Selecting single value in Linq: C#

Introduction

In this post, lets discuss Selecting single value in linq, such as :

  1. Numeric Result type
  2. Boolean result type
  3. Type <T> result type

Numeric Result type

These operators returns single numeric value, when applied on collection.

Average()

It Calculates the average of elements value of collection. For example:

Max()

It Calculates the The maximum value in the collection. For e.g., when we want to calcualtes maximum salary of employee collection

Min()

It Calculates the The minimum value in the collection.

Sum()

It Calculates the The sum of The sum of the projected values from collection.

Count()

The number of elements in the Collection, It has two overloaded version:

LongCount()

it is similarly as Count(), but it return long(Int64) value, because Count() returns Int32 and some collection may be larger which is overflow in case of Count().

It also has two overloaded version:

Boolean result type

Any()

It also has overloaded version. true if the source sequence contains any elements; otherwise, false.

It has two overloaded version:

All()

True if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.

Contains()

It checks for element in collection sequence.
For example:

SequenceEqual

determines whether two sequences are equal by comparing the elements by using the default equality comparer for their type.

Type <T> result type

These operators return specific type as type of collection or we explicitly assign type.

ElementAt()

It Returns the element at a specified index in a sequence.

ElementAtOrDefault

Returns the element at a specified index in a sequence or a default value if the index is out of range.

First

It Returns the first element of a collection. It has two overloaded methods:

FirstOrDefault

Returns the first element of a sequence, or a default value if the collection contains no elements.

Last

Returns the last element of a sequence. It also has two overloaded method

LastOrDefault

Returns the last element of a sequence, or a default value if the sequence contains no elements

Single

Returns the only element of a sequence, and throws an exception if there is not exactly one element in the sequence.

It throws an error, when the input collection contains more than one element.-or-The input sequence is empty.

SingleOrDefault

Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.

It raise exception when collection contains more than one element.

DefaultIfEmpty

Returns the elements of the specified sequence or the type parameter’s default value in a singleton collection if the sequence is empty.

Summary

In this post I discussed various operator and thier result types.

Please leave a comment, it encourage me to write quality articles.

Happy reading and Coding 🙂

Advertisements

One thought on “Selecting single value in Linq: C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s