text stringlengths 0 13.4k |
|---|
seed: new StringBuilder(), |
func: (seed, element) => seed.Append($"{element},")); |
Console.WriteLine(commaSeparatedElements2.ToString()); //1,2,3,4,5, |
Using a result selector: |
var commaSeparatedElements3 = elements.Aggregate( |
seed: new StringBuilder(), |
func: (seed, element) => seed.Append($"{element},"), |
resultSelector: (seed) => seed.ToString()); |
Console.WriteLine(commaSeparatedElements3); //1,2,3,4,5, |
If a seed is omitted, the first element becomes the seed: |
GoalKicker.com – .NET Framework Notes for Professionals |
31 |
var seedAndElements = elements.Select(n=>n.ToString()); |
var commaSeparatedElements4 = seedAndElements.Aggregate( |
func: (aggregate, element) => $"{aggregate}{element},"); |
Console.WriteLine(commaSeparatedElements4); //12,3,4,5, |
Section 8.8: ToLookup |
var persons = new[] { |
new { Name="Fizz", Job="Developer"}, |
new { Name="Buzz", Job="Developer"}, |
new { Name="Foo", Job="Astronaut"}, |
new { Name="Bar", Job="Astronaut"}, |
}; |
var groupedByJob = persons.ToLookup(p => p.Job); |
foreach(var theGroup in groupedByJob) |
{ |
Console.WriteLine( |
"{0} are {1}s", |
string.Join(",", theGroup.Select(g => g.Name).ToArray()), |
theGroup.Key); |
} |
//Fizz,Buzz are Developers |
//Foo,Bar are Astronauts |
Section 8.9: Intersect |
var numbers1to10 = new[] {1,2,3,4,5,6,7,8,9,10}; |
var numbers5to15 = new[] {5,6,7,8,9,10,11,12,13,14,15}; |
var numbers5to10 = numbers1to10.Intersect(numbers5to15); |
Console.WriteLine(string.Join(",", numbers5to10)); |
//5,6,7,8,9,10 |
Section 8.10: Concat |
var numbers1to5 = new[] {1, 2, 3, 4, 5}; |
var numbers4to8 = new[] {4, 5, 6, 7, 8}; |
var numbers1to8 = numbers1to5.Concat(numbers4to8); |
Console.WriteLine(string.Join(",", numbers1to8)); |
//1,2,3,4,5,4,5,6,7,8 |
Note that duplicates are kept in the result. If this is undesirable, use Union instead. |
Section 8.11: All |
var numbers = new[] {1,2,3,4,5}; |
var allNumbersAreOdd = numbers.All(n => (n & 1) == 1); |
GoalKicker.com – .NET Framework Notes for Professionals |
32 |
Console.WriteLine(allNumbersAreOdd); //False |
var allNumbersArePositive = numbers.All(n => n > 0); |
Console.WriteLine(allNumbersArePositive); //True |
Note that the All method functions by checking for the first element to evaluate as false according to the |
predicate. Therefore, the method will return true for any predicate in the case that the set is empty: |
var numbers = new int[0]; |
var allNumbersArePositive = numbers.All(n => n > 0); |
Console.WriteLine(allNumbersArePositive); //True |
Section 8.12: Sum |
var numbers = new[] {1,2,3,4}; |
var sumOfAllNumbers = numbers.Sum(); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.