Concatenation and Aggregation
Concatenation operations append a sequence to another. LINQ utilizes the Concat method to perform concatenation operations. Review its syntax below:
public static IEnumerable<TSource> Concat<TSource>( this IEnumerable<TSource> first, IEnumerable<TSource> second )
Review an example of its use below:
class Rides { public string Name { get; set; } public int Year { get; set; } } static Rides[] GetClassics() { Rides[] classics = { new Rides { Name="Chevy", Year=66 }, new Rides { Name="Pontiac", Year=64 }, new Rides { Name="Porche", Year=60 } }; return classics; } static Rides[] GetMods() { Rides[] mods = { new Rides { Name="Chevy", Year=16 }, new Rides { Name="Porche", Year=16 }, new Rides { Name="Jaguar", Year=17 } }; return mods; } public static void ConcatOne() { Rides[] classics = GetClassics(); Rides[] mods = GetMods(); IEnumerable<string> query = classics.Select(classic => classic.Name).Concat(mods.Select(mod => mod.Name)); foreach (string name in query) { Console.WriteLine(name); } }
An alternative for concatenation involves the use of SelectMany. Create a sequence collection, such as an array, and then use the SelectMany method, passing the identity selector function. Review an example of this technique below:
Rides[] classics = GetClassics(); Rides[] mods = GetMods(); IEnumerable<string> query = new[] { classics.Select(classic => classic.Name), mods.Select(mod => mod.Name) } .SelectMany(name => name); foreach (string name in query) { Console.WriteLine(name); }
AGGREGATION
In an aggregation operation, a collection of values yields a single value such as calculating an average of quantities. LINQ utilizes seven methods in aggregation: Aggregate, Average, Count, LongCount, Max, Min, and Sum.
The Aggregate method executes custom aggregation operations on collection values. Review its syntax below:
public static TSource Aggregate<TSource>( this IEnumerable<TSource> source, Func<TSource, TSource, TSource> func )
Review an example of its use below:
string sentence = "Honey I tried to tell him that you were the marrying kind."; // Split into individual terms string[] terms = sentence.Split(' '); // Prepend terms to start of new sentence to reverse order string reversed = terms.Aggregate((workingBuild, next) => next + " " + workingBuild); Console.WriteLine(reversed);
The Average method calculates the average of a collection. Review its syntax below:
public static decimal Average( this IEnumerable<decimal> source )
Review an example of its use below:
List<int> rankings = new List<int> { 76, 83, 99, 65, 66 }; double average = rankings.Average(); Console.WriteLine("The average rank is {0}.", average);
The Count method counts the elements of a collection, or only the elements satisfying a predicate function. Review its syntax below:
public static int Count<TSource>( this IEnumerable<TSource> source )
Review an example of its use below:
string[] pianos = { "grand", "baby grand", "electric", "upright", "synth", "organ" }; try { int numberOfPianos = pianos.Count(); Console.WriteLine( "There are {0} pianos total.", numberOfPianos); } catch (OverflowException) { Console.WriteLine("The count exceeds Int32 storage."); Console.WriteLine("Use LongCount() instead."); }
The LongCount method counts the elements of large collection, or only the elements satisfying a predicate function. Review its syntax below:
public static long LongCount<TSource>( this IEnumerable<TSource> source )
Review an example of its use below:
string[] sports = { "surfing", "soccer", "swimming", "track", "basketball", "gymnastics" }; long count = sports.LongCount(); Console.WriteLine("There are {0} sports total.", count);
The Max method finds the maximum value within a collection. Review its syntax below:
public static decimal Max( this IEnumerable<decimal> source )
Review an example of its use below:
List<long> massive = new List<long> { 824984658296Z, 996649835Z, 4832115Z }; long max = massive.Max(); Console.WriteLine("The largest value follows: {0}.", max);
The Min method finds the minimum value within a collection. Review its syntax below:
public static decimal Min( this IEnumerable<decimal> source )
Review an example of its use below:
double[] DBLstuff = { 4.5E+101, 6E+109, -3E+108 }; double min = DBLstuff.Min(); Console.WriteLine("The smallest value follows: {0}.", min);
The Sum method calculates the sum of a collection's values. Review its syntax below:
public static decimal Sum( this IEnumerable<decimal> source )
Review an example of its use below:
List<float> values = new List<float> { 83.68F, 7.25F, 481.7F, 1.1F }; float sum = values.Sum(); Console.WriteLine("The sum of all values follows: {0}.", sum);