Public · Protected · Private
Aggregate functions in LINQ
Type: Public  |  Created: 2012-07-19  |  Frozen: Yes
« Previous Public Blog Next Public Blog »
Comments
  • int[] ints = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; // 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>
    2012-07-19 01:07
  • var myaverage = (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 ).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
    2012-07-19 01:15
  • int first = (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 ).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
    2012-07-19 01:46
  • 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();
    2012-07-19 01:55
This blog is frozen. No new comments or edits allowed.