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


C# System.Linq.Average方法代码示例

本文整理汇总了C#中System.Linq.Average方法的典型用法代码示例。如果您正苦于以下问题:C# System.Linq.Average方法的具体用法?C# System.Linq.Average怎么用?C# System.Linq.Average使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Linq的用法示例。


在下文中一共展示了System.Linq.Average方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Execute

        public void Execute()
        {
            var numbers = new[]
            {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            };

            //
            // Average拡張メソッドは、文字通り平均を求める拡張メソッド。
            //
            // Average拡張メソッドには、各基本型のオーバーロードが用意されており
            // (decimal, double, int, long, single及びそれぞれのNullable型)
            // それぞれに、引数無しとselectorを指定するバージョンのメソッドがある。
            //

            //
            // 引数無しのAverage拡張メソッドの使用.
            //
            Output.WriteLine("引数無し = {0}", numbers.Average());

            //
            // selectorを指定するAverage拡張メソッドの使用.
            //
            Output.WriteLine("引数有り = {0}", numbers.Average(item => item%2 == 0 ? item : 0));
        }
开发者ID:devlights,项目名称:Sazare,代码行数:25,代码来源:LinqSamples35.cs

示例2: Test


//.........这里部分代码省略.........
                        new { Group = (string)null, Min = 3000 }
                 };

            assert.DeepEqual(groupedMin, groupedMinExpected, "Min() within group");

            // TEST
            var groupedMinWithLet = (from p in Person.GetPersons()
                                     group p by p.Group into g
                                     let minCount = g.Min(x => x.Count)
                                     select new { Group = g.Key, Name = g.Where(x => x.Count == minCount).Select(x => x.Name).ToArray() })
                             .ToArray();

            var groupedMinWithLetExpected = new object[] {
                        new { Group = "A", Name = new[]{ "Frank"} },
                        new { Group = "C", Name = new[]{ "Zeppa"} },
                        new { Group = "B", Name = new[]{ "Dora"} },
                        new { Group = (string)null, Name = new[]{ "Nemo"} }
                 };

            assert.DeepEqual(groupedMinWithLet, groupedMinWithLetExpected, "Min() within group with let");

            // TEST
            int maxNum = numbers.Max();
            assert.DeepEqual(maxNum, 5, "Max() number");

            // TEST
            int longestWordLength = words.Max(w => w.Length);
            assert.DeepEqual(longestWordLength, 5, "Max() for longest word");

            // TEST
            var groupedMax = (from p in Person.GetPersons()
                              group p by p.Group into g
                              select new { Group = g.Key, Max = g.Max(x => x.Count) })
                          .ToArray();

            var groupedMaxExpected = new object[] {
                        new { Group = "A", Max = 300 },
                        new { Group = "C", Max = 500 },
                        new { Group = "B", Max = 700 },
                        new { Group = (string)null, Max = 3000 }
                 };

            assert.DeepEqual(groupedMax, groupedMaxExpected, "Max() within group");

            // TEST
            var groupedMaxWithLet = (from p in Person.GetPersons()
                                     group p by p.Group into g
                                     let maxCount = g.Max(x => x.Count)
                                     select new { Group = g.Key, Name = g.Where(x => x.Count == maxCount).Select(x => x.Name).ToArray() })
                             .ToArray();

            var groupedMaxWithLetExpected = new object[] {
                        new { Group = "A", Name = new[]{ "Frank"} },
                        new { Group = "C", Name = new[]{ "Billy"} },
                        new { Group = "B", Name = new[]{ "John", "Mary"} },
                        new { Group = (string)null, Name = new[]{ "Nemo"} }
                 };

            assert.DeepEqual(groupedMaxWithLet, groupedMaxWithLetExpected, "Max() within group with let");

            // TEST
            double averageNum = numbers.Average();
            assert.DeepEqual(averageNum, 2.125, "Average() number");

            // TEST
            var averageWordLengths = new[] { "1", "22", "333", "4444", "55555" };
            double averageWordLength = averageWordLengths.Average(w => w.Length);
            assert.DeepEqual(averageWordLength, 3, "Average() for word lengths");

            // TEST
            var groupedAverage = (from p in Person.GetPersons()
                                  group p by p.Group into g
                                  select new { Group = g.Key, Average = g.Average(x => x.Count) })
                         .ToArray();

            var groupedAverageExpected = new object[] {
                        new { Group = "A", Average = 300 },
                        new { Group = "C", Average = 300 },
                        new { Group = "B", Average = 500 },
                        new { Group = (string)null, Average = 3000 }
                 };

            assert.DeepEqual(groupedAverage, groupedAverageExpected, "Average() within group");

            // TEST
            var doublesForAggregate = new[] { 1.0, 2.0, 3.0, 4.0, 5.0 };
            double product = doublesForAggregate.Aggregate((runningProduct, nextFactor) => runningProduct * nextFactor);
            assert.DeepEqual(product, 120, "Aggregate() within doubles");

            // TEST
            var startBalance = 100.0;
            var attemptedWithdrawals = new[] { 20, 10, 40, 50, 10, 70, 30 };

            var endBalance =
                attemptedWithdrawals.Aggregate(startBalance,
                    (balance, nextWithdrawal) =>
                        ((nextWithdrawal <= balance) ? (balance - nextWithdrawal) : balance));

            assert.DeepEqual(endBalance, 20, "Aggregate() balance");
        }
开发者ID:RashmiPankaj,项目名称:Bridge,代码行数:101,代码来源:TestLinqAggregateOperators.cs


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