Aggregate functions in LINQ
// Declare our delegate FUNC methods.
Func<int, int> MultiplyWithThree = i => i * 3;
Func<int, bool> GreaterThanTwo = i => i > 2;
Func<int, int, int> MultiplyThem = (i, j) => i * j;
Func<double, double, double> MultiplyThemDoubles = (i, j) => i * j;
var myints = (from i in ints // one group
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).Aggregate(MultiplyThem); //gives 2*4*6
// multiplication requirement is defined inside our own method
Console.WriteLine(myints);
bool allareGreaterThan2 = (from i in ints // one group
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).All(GreaterThanTwo); //gives 2 > 2 && 4 > 2 & 6 > 2
Console.WriteLine(allareGreaterThan2.ToString());
bool AnyisGreaterThan2 = (from i in ints // one group
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).Any(GreaterThanTwo); //gives 2 > 2 || 4 > 2 || 6 > 2
Console.WriteLine(AnyisGreaterThan2.ToString());
IEnumerable<int> myenumerable =
(from i in ints // one group
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).ToArray() //gives a int[]
.AsEnumerable(); //gives IEnumerable<int>
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).Average(); //gives 2+4+6 /3
var mycasteddoubles = (from i in ints // one group
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).Cast<double>(); //gives 2,4,6 as doubles
bool doesithave2 = (from i in ints // one group
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).Contains(2); //gives true if it has 2 in {2,4,6}
int howmany = (from i in ints // one group
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).Count(); //gives count
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).ElementAt(0); //gives firstone
var distinctNumbers = (from i in ints // one group
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).Distinct(); //gives distinct
var first1 = (from i in ints // one group
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).ElementAtOrDefault(0); //gives firstone
var first2 = (from i in ints // one group
from j in ints // second group
where i == j && i < 4 // choose 1,2,3 only
select i + j // gives 2,4,6
).First( ); //gives firstone
var test = (from i in ints select i).Last();
test = (from i in ints select i).LastOrDefault();
test = (from i in ints select i).Max();
test = (from i in ints select i).Min();
int[] intarray = (from i in ints select i).ToArray();
List<int> intlist = (from i in ints select i).ToList();