Element Operations
Element operations return one specific element from a sequence.
The query operator methods utilized to perform element operations follow: ElementAt, ElementAtOrDefault, First, FirstOrDefault, Last, LastOrDefault, Single, and SingleOrDefault.
The ElementAt method returns an element at a specific index within a collection. Review its syntax below.
public static TSource ElementAt<TSource>( this IEnumerable<TSource> source, int index )
Review an example of its use below:
string[] teamMembers = { "Haart, Tommy", "Addams, Timmy", "Anderson, Henry T.", "Paisley, Martha", "Appiah, Geoffrey" }; Random random = new Random(DateTime.Now.Millisecond); string name = teamMembers.ElementAt(random.Next(0, teamMembers.Length)); Console.WriteLine("A random team member follows '{0}'.", name);
The ElementAtOrDefault method either returns an element at a specific index within a collection, or returns a default value if the index exists out of range. Review its syntax below.
public static TSource ElementAtOrDefault<TSource>( this IEnumerable<TSource> source, int index )
Review an example of its use below:
string[] staff = { "Carter, Thomas", "Adley, Cary", "Anderson, Henry T.", "Heder, Maggie", "Ishikawa, Jeff" }; int index = 30; string assoc = staff.ElementAtOrDefault(index); Console.WriteLine( "The name at index {0} follows: '{1}'.", index, String.IsNullOrEmpty(assoc) ? "<no name at this index>" : assoc);
The First method either returns a collection's first element, or returns the first element satisfying the given condition. Review its syntax below.
public static TSource First<TSource>( this IEnumerable<TSource> source )
Review an example of its use below:
int[] values = { 8, 11, 62, 82, 77, 135, 2, 44, 81, 24, 17, 405, 60, 312, 16 }; int first = values.First(); Console.WriteLine(first);
The FirstOrDefault method performs one of three tasks: returns a collection's first element, returns the first element satisfying a given condition, or returns a default value due to the absence of the required element. Review its syntax below.
public static TSource FirstOrDefault<TSource>( this IEnumerable<TSource> source )
Review an example of its use below:
int[] values = { }; int first = values.FirstOrDefault(); Console.WriteLine(first);
In some applications, the default value is undesirable in the event of detecting an empty collection. The DefaultIfEmpty method allows for specification of the preferred default value on discovery of an empty collection, rather than checking for an unwanted default and then making changes. Review an example of its use below:
List<int> months1 = new List<int> { }; // Set default value to 1 after query execution int monthOne = months1.FirstOrDefault(); if (monthOne == 0) { monthOne = 1; } Console.WriteLine("The monthOne variable value follows {0}", monthOne); // Set default value to 1, using DefaultIfEmpty() int monthTwo = months1.DefaultIfEmpty(1).First(); Console.WriteLine("The monthTwo variable value follows {0}", monthTwo);
The Last method returns a collection's last element, or the last element satisfying a given condition. Review its syntax below.
public static TSource Last<TSource>( this IEnumerable<TSource> source )
Review an example of its use below:
int[] values = { 7, 44, 60, 90, 81, 405, 4, 64, 63, 22, 87, 67, 12, 21 }; int LastV = values.Last(); Console.WriteLine(LastV);
The LastOrDefault method performs one of three tasks: returns the last element of a collection, returns the last element satisfying a given condition, or returns a default value due to the absence of the required element. Review its syntax below.
public static TSource LastOrDefault<TSource>( this IEnumerable<TSource> source )
Review an example of its use below:
string[] snacks = { }; string lastEntry = snacks.LastOrDefault(); Console.WriteLine( String.IsNullOrEmpty(lastEntry) ? "<string is null or empty>" : lastEntry);
Note the default value proves undesirable in some applications in the event of detecting an empty collection. The DefaultIfEmpty method allows for specification of the preferred default value on discovery of an empty collection, rather than checking for an unwanted default and then making changes.
The Single method returns either the lone element of a collection, or the only element satisfying a given condition. Review its syntax below.
public static TSource Single<TSource>( this IEnumerable<TSource> source )
Review an example of its use below:
string[] snacks = { "cashews" }; string snack1 = snacks.Single(); Console.WriteLine(snack1);
The SingleOrDefault method performs one of three tasks: returns the lone element of a collection, returns the only element satisfying a given condition, or returns a default value in the absence of the required element or if the collection holds more than a single element. Review its syntax below.
public static TSource SingleOrDefault<TSource>( this IEnumerable<TSource> source )
Review an example of its use below:
string[] snacks = { "chips" }; string snack1 = snacks.SingleOrDefault(); Console.WriteLine(snack1);
Note the DefaultIfEmpty method allows for specification of the preferred default value on discovery of an empty collection.