# Tutorial by Examples

## Min

var numbers = new[] {1,2,3,4}; var minNumber = numbers.Min(); Console.WriteLine(minNumber); //1 var cities = new[] { new {Population = 1000}, new {Population = 2500}, new {Population = 4000} }; var minPopulation = cities.Min(c =&gt; c.Population); Console.WriteLine(minPopu...

## Average

var numbers = new[] {1,2,3,4}; var averageNumber = numbers.Average(); Console.WriteLine(averageNumber); // 2,5 This method calculates the average of enumerable of numbers. var cities = new[] { new {Population = 1000}, new {Population = 2000}, new {Population = 4000} }; v...

## Zip

.NET4.0 var tens = new[] {10,20,30,40,50}; var units = new[] {1,2,3,4,5}; var sums = tens.Zip(units, (first, second) =&gt; first + second); Console.WriteLine(string.Join(&quot;,&quot;, sums)); //11,22,33,44,55

## Distinct

var numbers = new[] {1, 1, 2, 2, 3, 3, 4, 4, 5, 5}; var distinctNumbers = numbers.Distinct(); Console.WriteLine(string.Join(&quot;,&quot;, distinctNumbers)); //1,2,3,4,5

## GroupBy

var persons = new[] { new { Name=&quot;Fizz&quot;, Job=&quot;Developer&quot;}, new { Name=&quot;Buzz&quot;, Job=&quot;Developer&quot;}, new { Name=&quot;Foo&quot;, Job=&quot;Astronaut&quot;}, new { Name=&quot;Bar&quot;, Job=&quot;Astronaut&quot;}, }; var groupedByJob = person...

## ToDictionary

Returns a new dictionary from the source IEnumerable using the provided keySelector function to determine keys. Will throw an ArgumentException if keySelector is not injective(returns a unique value for each member of the source collection.) There are overloads which allow one to specify the value t...

## Union

var numbers1to5 = new[] {1,2,3,4,5}; var numbers4to8 = new[] {4,5,6,7,8}; var numbers1to8 = numbers1to5.Union(numbers4to8); Console.WriteLine(string.Join(&quot;,&quot;, numbers1to8)); //1,2,3,4,5,6,7,8 Note that duplicates are removed from the result. If this is undesirable, use Concat ...

## Count

IEnumerable&lt;int&gt; numbers = new[] {1,2,3,4,5,6,7,8,9,10}; var numbersCount = numbers.Count(); Console.WriteLine(numbersCount); //10 var evenNumbersCount = numbers.Count(n =&gt; (n &amp; 1) == 0); Console.WriteLine(evenNumbersCount); //5

## ElementAt

var names = new[] {&quot;Foo&quot;,&quot;Bar&quot;,&quot;Fizz&quot;,&quot;Buzz&quot;}; var thirdName = names.ElementAt(2); Console.WriteLine(thirdName); //Fizz //The following throws ArgumentOutOfRangeException var minusOnethName = names.ElementAt(-1); var fifthName = names.ElementAt(4); ...

## ElementAtOrDefault

var names = new[] {&quot;Foo&quot;,&quot;Bar&quot;,&quot;Fizz&quot;,&quot;Buzz&quot;}; var thirdName = names.ElementAtOrDefault(2); Console.WriteLine(thirdName); //Fizz var minusOnethName = names.ElementAtOrDefault(-1); Console.WriteLine(minusOnethName); //null var fifthName = names.Eleme...

## SkipWhile

var numbers = new[] {2,4,6,8,1,3,5,7}; var oddNumbers = numbers.SkipWhile(n =&gt; (n &amp; 1) == 0); Console.WriteLine(string.Join(&quot;,&quot;, oddNumbers.ToArray())); //1,3,5,7

## TakeWhile

var numbers = new[] {2,4,6,1,3,5,7,8}; var evenNumbers = numbers.TakeWhile(n =&gt; (n &amp; 1) == 0); Console.WriteLine(string.Join(&quot;,&quot;, evenNumbers.ToArray())); //2,4,6

## DefaultIfEmpty

var numbers = new[] {2,4,6,8,1,3,5,7}; var numbersOrDefault = numbers.DefaultIfEmpty(); Console.WriteLine(numbers.SequenceEqual(numbersOrDefault)); //True var noNumbers = new int[0]; var noNumbersOrDefault = noNumbers.DefaultIfEmpty(); Console.WriteLine(noNumbersOrDefault.Count()); //1 C...

## Aggregate (fold)

Generating a new object in each step: var elements = new[] {1,2,3,4,5}; var commaSeparatedElements = elements.Aggregate( seed: &quot;&quot;, func: (aggregate, element) =&gt; \$&quot;{aggregate}{element},&quot;); Console.WriteLine(commaSeparatedElements); //1,2,3,4,5, Using th...

## ToLookup

var persons = new[] { new { Name=&quot;Fizz&quot;, Job=&quot;Developer&quot;}, new { Name=&quot;Buzz&quot;, Job=&quot;Developer&quot;}, new { Name=&quot;Foo&quot;, Job=&quot;Astronaut&quot;}, new { Name=&quot;Bar&quot;, Job=&quot;Astronaut&quot;}, }; var groupedByJob = person...

## Join

class Developer { public int Id { get; set; } public string Name { get; set; } } class Project { public int DeveloperId { get; set; } public string Name { get; set; } } var developers = new[] { new Developer { Id = 1, Name = &quot;Foobuzz&quot; ...

## GroupJoin

class Developer { public int Id { get; set; } public string Name { get; set; } } class Project { public int DeveloperId { get; set; } public string Name { get; set; } } var developers = new[] { new Developer { Id = 1, Name = &quot;Foobuzz&quot; ...

## as

The as keyword is an operator similar to a cast. If a cast is not possible, using as produces null rather than resulting in an InvalidCastException. expression as type is equivalent to expression is type ? (type)expression : (type)null with the caveat that as is only valid on reference conversions,...

Page 6 of 1336