LINQ之Sum

返回LINQ大全首页

Sum()

MSDN

Sum()易于使用。例如int类型和float类型可以直接通过调用Sum()来获得总值。

public static class Program
{
    static void Main( string[] args )
    {
        int[]       intNumbers      = new int[]     { 1, 4, 3, 4 };
        float[]     floatNumbers    = new float[]   { 1.1f, 3.3f, 6.0f };

        int     intSum      = intNumbers.Sum();
        float   floatSum    = floatNumbers.Sum();

        System.Console.WriteLine( "intNumbers:{0}",   intNumbers.Text() );
        System.Console.WriteLine( "合計:{0}",         intSum );

        System.Console.WriteLine( "floatNumbers:{0}",   floatNumbers.Text() );
        System.Console.WriteLine( "合計:{0}",           floatSum );

        System.Console.ReadKey();
    }

    public static string Text( this IEnumerable i_source )
    {
        string text = string.Empty;
        foreach( var value in i_source )
        {
            text += string.Format( "[{0}], ", value );
        }
        return text;
    }

}
intNumbers:[1], [4], [3], [4],
合計:12
floatNumbers:[1.1], [3.3], [6],
合計:10.4

Sum()也支持可空类型。

public static class Program
{
    static void Main( string[] args )
    {
        int?[]      intNumbers      = new int?[]    { 1, null, 3, null };
        float?[]    floatNumbers    = new float?[]  { null, null, null };

        int?    intSum      = intNumbers.Sum();
        float?  floatSum    = floatNumbers.Sum();

        System.Console.WriteLine( "intNumbers:{0}",   intNumbers.Text() );
        System.Console.WriteLine( "合計:{0}",         intSum );

        System.Console.WriteLine( "floatNumbers:{0}",   floatNumbers.Text() );
        System.Console.WriteLine( "合計:{0}",           floatSum );

        System.Console.ReadKey();
    }

    public static string Text( this IEnumerable i_source )
    {
        string text = string.Empty;
        foreach( var value in i_source )
        {
            text += string.Format( "[{0}], ", value );
        }
        return text;
    }

}
intNumbers:[1], , [3], ,
合計:4
floatNumbers:, , [],
合計:0

Sum()可以通过lambda表达式求得类中数据的和。

public static class Program
{
    private class Parameter
    {
        public string   Name    { get; set; }
        public int      Age     { get; set; }
        
        public override string ToString()
        {
            return string.Format( "Name:{0}, Age:{1}", Name, Age );
        }
    }

    static void Main( string[] args )
    {
        // 年齢と名前データ。
        Parameter[] parameters = new Parameter[]
        {
            new Parameter() { Age = 52, Name = "正一郎" },
            new Parameter() { Age = 28, Name = "清次郎" },
            new Parameter() { Age = 20, Name = "誠三郎" },
            new Parameter() { Age = 18, Name = "征史郎" },
        };

        int ageSum = parameters.Sum( value => value.Age );

        // 結果発表
        System.Console.WriteLine( "parameters:{0}", parameters.Text() );
        System.Console.WriteLine( "年齢の合計:{0}", ageSum );

        // 入力待ち用
        System.Console.ReadKey();
    }

    /// 
    /// 簡易的なシーケンスのテキスト取得処理
    /// 
    public static string Text( this IEnumerable i_source )
    {
        string text = string.Empty;
        foreach( var value in i_source )
        {
            text += string.Format( "[{0}], ", value );
        }
        return text;
    }

} // class Program
parameters:[Name:正一郎, Age:52], [Name:清次郎, Age:28], [Name:誠三郎, Age:20],
[Name:征史郎, Age:18],
年齢の合計:118

你可能感兴趣的:(C#,c#,.net,linq)