Материал из .Net Framework эксперт
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Aggregate and Sum
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
IEnumerable<int> intSequence = Enumerable.Range(1, 10);
foreach (int item in intSequence)
Console.WriteLine(item);
int sum = intSequence.Aggregate(0, (s, i) => s + i);
Console.WriteLine(sum);
}
}
Aggregate for string length
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
string[] curries = { "abc", "ab", "abcd" };
Console.WriteLine(curries.Aggregate<string, string, int>(
"Some curries:",
(a, b) => a + " " + b,
a => a.Length));
}
}
Aggregate Prototype
using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
public class MainClass {
public static void Main() {
int N = 5;
IEnumerable<int> intSequence = Enumerable.Range(1, N);
foreach (int item in intSequence)
Console.WriteLine(item);
int agg = intSequence.Aggregate((av, e) => av * e);
Console.WriteLine("{0}! = {1}", N, agg);
}
}
Aggregate string and int value
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
string[] curries = { "abc", "abcd", "ab" };
Console.WriteLine(curries.Aggregate<string, int>(0,(a, b) => a + b.Length));
}
}
Aggregate three values
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
string[] curries = { "abc", "abcd", "ab" };
Console.WriteLine(curries.Aggregate<string, string, string>(
"curries:",
(a, b) => a + " " + b,
a => a));
}
}
Aggregate two string values
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class Program
{
static void Main(string[] args)
{
string[] curries = { "abc", "abcd", "ab" };
Console.WriteLine(curries.Aggregate((a, b) => a + " " + b));
}
}
Use Aggregate on an array
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class MainClass {
public static void Main() {
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var query = numbers.Aggregate((a, b) => a * b);
}
}
Use Aggregate on an array with tenary operator
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class MainClass{
public static void Main(){
int[] numbers = { 9, 3, 5, 4, 2, 6, 7, 1, 8 };
var query = numbers.Aggregate(5, (a,b) => ( (a < b) ? (a * b) : a));
}
}