本文整理汇总了C#中Animal.GroupBy方法的典型用法代码示例。如果您正苦于以下问题:C# Animal.GroupBy方法的具体用法?C# Animal.GroupBy怎么用?C# Animal.GroupBy使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Animal
的用法示例。
在下文中一共展示了Animal.GroupBy方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetAverageAge
public static IDictionary<string, decimal> GetAverageAge(Animal[] animals)
{
return animals.GroupBy(x => x.GetType()).ToDictionary(
x => x.Key.Name,
x => x.Average(y => y.Age)
);
}
示例2: Main
static void Main()
{
Frog f1 = new Frog("Billy", 0.2, "m");
Dog d1 = new Dog("Liza", 4, "f");
Cat c1 = new Cat("Kotaksi", 2, "m");
Kitten k1 = new Kitten("Raya", 1);
Tomcat t1 = new Tomcat("Rijo", 2);
f1.ProduceSound();
d1.ProduceSound();
c1.ProduceSound();
k1.ProduceSound();
t1.ProduceSound();
Console.WriteLine();
Animal[] animals = new Animal[]
{
new Frog("Billy", 0.2, "m"),
new Dog("Liza", 4, "f"),
new Cat("Kotaksi", 2, "m"),
new Kitten("Raya", 1),
new Tomcat("Rijo", 2),
new Frog("Pesho", 2.1, "f"),
new Dog("Beti", 5.4, "f"),
new Cat("Kotka", 2, "f"),
new Kitten("Spaska", 4),
new Tomcat("Gosho", 3),
new Frog("Marmot", 4, "m"),
new Tomcat("Bizen", 0.5),
new Dog("India", 2.5, "f"),
};
animals.
GroupBy(animal => animal.GetType().Name).
Select(group => new
{
name = group.Key,
averageYears = group.Average(animal => animal.Age)
}).
ToList().
ForEach(group => Console.WriteLine("Group: {0}, average age: {1:F2} years!", group.name, group.averageYears));
}
示例3: Main
static void Main()
{
var animals = new Animal[]
{
new Dog("Sharo", 5),
new Frog("Frogy", 10),
new Cat("Masha", 11),
new TomCat("Tom", 2, Gender.Male),
new Kitten("Kitty", 3, Gender.Female)
};
animals.ToList().ForEach(Console.WriteLine);
//var animalsBygroups = animals.GroupBy(GetAnimalKind,
// (g, a) => new { kind = g, averagAge = a.Average(animal => animal.Age) });
var animalsBygroups = animals.GroupBy(GetAnimalKind,
(g, a) => new { kind = g, averagAge = a.Average(animal => animal.Age) });
foreach (var animalGroup in animalsBygroups)
{
Console.WriteLine("The average age of {0}s is {1:f2}.", animalGroup.kind, animalGroup.averagAge);
}
}
示例4: Main
static void Main()
{
var tom = new Tomcat("Tommas", 3, "m");
var bruno = new Tomcat("Bruno", 1, "male");
var spike = new Dog("Spike", 4, "m");
var dog = new Dog("Бенджи", 4, "male");
var frog = new Frog("Veso", 1, "female");
var anotherFrog = new Frog("Sasho", 2, "m");
var kitten = new Kitten("Kote", 2, "female");
var cat = new Cat("Маца", 5, "f");
var anotherKitty = new Kitten("Penka", 4, "female");
Animal[] animals = new Animal[]
{
tom, dog, frog, kitten, cat, spike, bruno, anotherFrog, anotherKitty
};
foreach (var kind in animals.GroupBy(animal => animal.GetType()))
{
double averageAge = kind.Average(animal => (double)animal.Age);
Console.WriteLine("{0} - average age: {1}", kind.Key, averageAge);
}
}
示例5: Main
static void Main()
{
var animals = new Animal[]
{
new Dog("Spot", 2, Genter.Male),
new Cat("Pisana", 1, Genter.Femail),
new Frog("Jabcho", 1, Genter.Male),
new Kitten("Kity", 5),
new Tomcat("Tom", 4)
};
animals.ToList().ForEach(Console.WriteLine);
Console.WriteLine();
var animalsBygroups = animals.GroupBy(GetAnimalKind,
(g, a) => new { kind = g, averagAge = a.Average(animal => animal.Age) });
foreach (var animalGroup in animalsBygroups)
{
Console.WriteLine("The average age of {0}s is {1:f2}.", animalGroup.kind, animalGroup.averagAge);
}
}