当前位置: 首页>>代码示例>>C#>>正文


C# Animal.GroupBy方法代码示例

本文整理汇总了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)
     );
 }
开发者ID:RuParusheva,项目名称:TelerikAcademy,代码行数:7,代码来源:Animal.cs

示例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));
        }
开发者ID:imurtov,项目名称:InheritanceAndAbstractionHomework,代码行数:43,代码来源:Animals.cs

示例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);
        }
    }
开发者ID:ScreeM92,项目名称:Software-University,代码行数:22,代码来源:AnimalsMainClass.cs

示例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);
        }
    }
开发者ID:alvelchev,项目名称:SoftUni,代码行数:23,代码来源:AnimalsTest.cs

示例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);
        }

    }
开发者ID:ScreeM92,项目名称:Software-University,代码行数:24,代码来源:AnimalsMainClass.cs


注:本文中的Animal.GroupBy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。