Enumerable.Sum 方法

定义

计算数值序列的和。

重载

Sum(IEnumerable<Single>)

计算 Single 值序列的总和。

Sum(IEnumerable<Nullable<Int32>>)

计算可以为 null 的 Int32 值序列的总和。

Sum(IEnumerable<Nullable<Single>>)

计算可以为 null 的 Single 值序列的总和。

Sum(IEnumerable<Nullable<Int64>>)

计算可以为 null 的 Int64 值序列的总和。

Sum(IEnumerable<Nullable<Double>>)

计算可以为 null 的 Double 值序列的总和。

Sum(IEnumerable<Int32>)

计算 Int32 值序列的总和。

Sum(IEnumerable<Int64>)

计算 Int64 值序列的总和。

Sum(IEnumerable<Double>)

计算 Double 值序列的总和。

Sum(IEnumerable<Decimal>)

计算 Decimal 值序列的总和。

Sum(IEnumerable<Nullable<Decimal>>)

计算可以为 null 的 Decimal 值序列的总和。

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Single>)

计算 Single 值序列的总和,这些值可通过对输入序列中的每个元素调用转换函数获得。

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)

计算可以为 null 的 Int64 值序列的总和,这些值可通过对输入序列的每个元素调用转换函数获得。

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)

计算可以为 null 的 Int32 值序列的总和,这些值可通过对输入序列的每个元素调用转换函数获得。

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)

计算可以为 null 的 Double 值序列的总和,这些值可通过对输入序列的每个元素调用转换函数获得。

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)

计算可以为 null 的 Single 值序列的总和,这些值可通过对输入序列的每个元素调用转换函数获得。

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)

计算 Int64 值序列的总和,这些值可通过对输入序列中的每个元素调用转换函数获得。

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)

计算 Int32 值序列的总和,这些值可通过对输入序列中的每个元素调用转换函数获得。

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>)

计算 Double 值序列的总和,这些值可通过对输入序列中的每个元素调用转换函数获得。

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)

计算 Decimal 值序列的总和,这些值可通过对输入序列中的每个元素调用转换函数获得。

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)

计算可以为 null 的 Decimal 值序列的总和,这些值可通过对输入序列的每个元素调用转换函数获得。

Sum(IEnumerable<Single>)

计算 Single 值序列的总和。

public:
[System::Runtime::CompilerServices::Extension]
 static float Sum(System::Collections::Generic::IEnumerable<float> ^ source);
public static float Sum (this System.Collections.Generic.IEnumerable<float> source);
static member Sum : seq<single> -> single
<Extension()>
Public Function Sum (source As IEnumerable(Of Single)) As Single

参数

source
IEnumerable<Single>

要计算其总和的 Single 值序列。

返回

序列中的值的总和。

例外

sourcenull

示例

下面的代码示例演示如何使用 Sum(IEnumerable<Single>) 对序列的值求和。

List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };

float sum = numbers.Sum();

Console.WriteLine("The sum of the numbers is {0}.", sum);

/*
 This code produces the following output:

 The sum of the numbers is 635.13.
*/
' Create a list of Single values.
Dim numbers As New List(Of Single)(New Single() _
                               {43.68F, 1.25F, 583.7F, 6.5F})

' Get the sum of values in the list.
Dim sum As Single = numbers.Sum()

' Display the output.
Console.WriteLine($"The sum of the numbers is {sum}")

' This code produces the following output:
'
' The sum of the numbers is 635.13

注解

如果 source 不包含任何元素,则此方法返回零。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum(IEnumerable<Nullable<Int32>>)

计算可以为 null 的 Int32 值序列的总和。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<int> Sum(System::Collections::Generic::IEnumerable<Nullable<int>> ^ source);
public static int? Sum (this System.Collections.Generic.IEnumerable<int?> source);
static member Sum : seq<Nullable<int>> -> Nullable<int>
<Extension()>
Public Function Sum (source As IEnumerable(Of Nullable(Of Integer))) As Nullable(Of Integer)

参数

source
IEnumerable<Nullable<Int32>>

要计算其总和的可以为 null 的 Int32 值序列。

返回

序列中的值的总和。

例外

sourcenull

注解

中的sourcenull项从总和的计算中排除。 如果 source 不包含任何元素或所有元素均为 null,则此方法返回零。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum(IEnumerable<Nullable<Single>>)

计算可以为 null 的 Single 值序列的总和。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<float> Sum(System::Collections::Generic::IEnumerable<Nullable<float>> ^ source);
public static float? Sum (this System.Collections.Generic.IEnumerable<float?> source);
static member Sum : seq<Nullable<single>> -> Nullable<single>
<Extension()>
Public Function Sum (source As IEnumerable(Of Nullable(Of Single))) As Nullable(Of Single)

参数

source
IEnumerable<Nullable<Single>>

要计算其总和的可以为 null 的 Single 值序列。

返回

序列中的值的总和。

例外

sourcenull

示例

下面的代码示例演示如何使用 Sum(IEnumerable<Nullable<Single>>) 对序列的值求和。

float?[] points = { null, 0, 92.83F, null, 100.0F, 37.46F, 81.1F };

float? sum = points.Sum();

Console.WriteLine("Total points earned: {0}", sum);

/*
 This code produces the following output:

 Total points earned: 311.39
*/
' Create an array of Nullable Single values.
Dim points() As Nullable(Of Single) =
{Nothing, 0, 92.83F, Nothing, 100.0F, 37.46F, 81.1F}

' Get the sum of values in the list.
Dim sum As Nullable(Of Single) = points.Sum()

' Display the output.
Console.WriteLine($"Total points earned: {sum}")

' This code produces the following output:
'
' Total points earned: 311.39

注解

中的sourcenull项从总和的计算中排除。 如果 source 不包含任何元素或所有元素均为 null,则此方法返回零。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum(IEnumerable<Nullable<Int64>>)

计算可以为 null 的 Int64 值序列的总和。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<long> Sum(System::Collections::Generic::IEnumerable<Nullable<long>> ^ source);
public static long? Sum (this System.Collections.Generic.IEnumerable<long?> source);
static member Sum : seq<Nullable<int64>> -> Nullable<int64>
<Extension()>
Public Function Sum (source As IEnumerable(Of Nullable(Of Long))) As Nullable(Of Long)

参数

source
IEnumerable<Nullable<Int64>>

要计算其总和的可以为 null 的 Int64 值序列。

返回

序列中的值的总和。

例外

sourcenull

注解

中的sourcenull项从总和的计算中排除。 如果 source 不包含任何元素或所有元素均为 null,则此方法返回零。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum(IEnumerable<Nullable<Double>>)

计算可以为 null 的 Double 值序列的总和。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<double> Sum(System::Collections::Generic::IEnumerable<Nullable<double>> ^ source);
public static double? Sum (this System.Collections.Generic.IEnumerable<double?> source);
static member Sum : seq<Nullable<double>> -> Nullable<double>
<Extension()>
Public Function Sum (source As IEnumerable(Of Nullable(Of Double))) As Nullable(Of Double)

参数

source
IEnumerable<Nullable<Double>>

要计算其总和的可以为 null 的 Double 值序列。

返回

序列中的值的总和。

例外

sourcenull

注解

中的sourcenull项从总和的计算中排除。 如果 source 不包含任何元素或所有元素均为 null,则此方法返回零。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum(IEnumerable<Int32>)

计算 Int32 值序列的总和。

public:
[System::Runtime::CompilerServices::Extension]
 static int Sum(System::Collections::Generic::IEnumerable<int> ^ source);
public static int Sum (this System.Collections.Generic.IEnumerable<int> source);
static member Sum : seq<int> -> int
<Extension()>
Public Function Sum (source As IEnumerable(Of Integer)) As Integer

参数

source
IEnumerable<Int32>

要计算其总和的 Int32 值序列。

返回

序列中的值的总和。

例外

sourcenull

注解

如果 source 不包含任何元素,则此方法返回零。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum(IEnumerable<Int64>)

计算 Int64 值序列的总和。

public:
[System::Runtime::CompilerServices::Extension]
 static long Sum(System::Collections::Generic::IEnumerable<long> ^ source);
public static long Sum (this System.Collections.Generic.IEnumerable<long> source);
static member Sum : seq<int64> -> int64
<Extension()>
Public Function Sum (source As IEnumerable(Of Long)) As Long

参数

source
IEnumerable<Int64>

要计算其总和的 Int64 值序列。

返回

序列中的值的总和。

例外

sourcenull

注解

如果 source 不包含任何元素,则此方法返回零。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum(IEnumerable<Double>)

计算 Double 值序列的总和。

public:
[System::Runtime::CompilerServices::Extension]
 static double Sum(System::Collections::Generic::IEnumerable<double> ^ source);
public static double Sum (this System.Collections.Generic.IEnumerable<double> source);
static member Sum : seq<double> -> double
<Extension()>
Public Function Sum (source As IEnumerable(Of Double)) As Double

参数

source
IEnumerable<Double>

要计算其总和的 Double 值序列。

返回

序列中的值的总和。

例外

sourcenull

注解

如果 source 不包含任何元素,则此方法返回零。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum(IEnumerable<Decimal>)

计算 Decimal 值序列的总和。

public:
[System::Runtime::CompilerServices::Extension]
 static System::Decimal Sum(System::Collections::Generic::IEnumerable<System::Decimal> ^ source);
public static decimal Sum (this System.Collections.Generic.IEnumerable<decimal> source);
static member Sum : seq<decimal> -> decimal
<Extension()>
Public Function Sum (source As IEnumerable(Of Decimal)) As Decimal

参数

source
IEnumerable<Decimal>

要计算其总和的 Decimal 值序列。

返回

序列中的值的总和。

例外

sourcenull

注解

如果source不包含任何元素,则Sum(IEnumerable<Decimal>)方法返回零。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum(IEnumerable<Nullable<Decimal>>)

计算可以为 null 的 Decimal 值序列的总和。

public:
[System::Runtime::CompilerServices::Extension]
 static Nullable<System::Decimal> Sum(System::Collections::Generic::IEnumerable<Nullable<System::Decimal>> ^ source);
public static decimal? Sum (this System.Collections.Generic.IEnumerable<decimal?> source);
static member Sum : seq<Nullable<decimal>> -> Nullable<decimal>
<Extension()>
Public Function Sum (source As IEnumerable(Of Nullable(Of Decimal))) As Nullable(Of Decimal)

参数

source
IEnumerable<Nullable<Decimal>>

要计算其总和的可以为 null 的 Decimal 值序列。

返回

序列中的值的总和。

例外

sourcenull

注解

中的sourcenull项从总和的计算中排除。 如果 source 不包含任何元素或所有元素均为 null,则此方法返回零。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Single>)

计算 Single 值序列的总和,这些值可通过对输入序列中的每个元素调用转换函数获得。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static float Sum(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, float> ^ selector);
public static float Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,float> selector);
static member Sum : seq<'Source> * Func<'Source, single> -> single
<Extension()>
Public Function Sum(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Single)) As Single

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

用于计算总和的值序列。

selector
Func<TSource,Single>

应用于每个元素的转换函数。

返回

投影值的总和。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 对序列的投影值求和。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
}

public static void SumEx1()
{
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", Weight = 25.2 },
              new Package { Company = "Lucerne Publishing", Weight = 18.7 },
              new Package { Company = "Wingtip Toys", Weight = 6.0 },
              new Package { Company = "Adventure Works", Weight = 33.8 } };

    double totalWeight = packages.Sum(pkg => pkg.Weight);

    Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}

/*
 This code produces the following output:

 The total weight of the packages is: 83.7
*/
Structure Package
    Public Company As String
    Public Weight As Double
End Structure

Sub SumEx1()
    ' Create a list of Package values.
    Dim packages As New List(Of Package)(New Package() _
     {New Package With {.Company = "Coho Vineyard", .Weight = 25.2},
      New Package With {.Company = "Lucerne Publishing", .Weight = 18.7},
      New Package With {.Company = "Wingtip Toys", .Weight = 6.0},
      New Package With {.Company = "Adventure Works", .Weight = 33.8}})

    ' Sum the values from each item's Weight property.
    Dim totalWeight As Double = packages.Sum(Function(pkg) _
                                             pkg.Weight)

    ' Display the result.
    Console.WriteLine($"The total weight of the packages is: {totalWeight}")
End Sub

' This code produces the following output:
'
' The total weight of the packages is: 83.7

注解

如果source不包含任何元素,则Sum<TSource>(IEnumerable<TSource>, Func<TSource,Single>)方法返回零。

如果提供将 的成员source投影为数值类型的函数 selector,则可以将此方法应用于任意值序列,具体来说就是 Single

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int64>>)

计算可以为 null 的 Int64 值序列的总和,这些值可通过对输入序列的每个元素调用转换函数获得。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<long> Sum(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<long>> ^ selector);
public static long? Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,long?> selector);
static member Sum : seq<'Source> * Func<'Source, Nullable<int64>> -> Nullable<int64>
<Extension()>
Public Function Sum(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Long))) As Nullable(Of Long)

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

用于计算总和的值序列。

selector
Func<TSource,Nullable<Int64>>

应用于每个元素的转换函数。

返回

投影值的总和。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 对序列的投影值求和。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
}

public static void SumEx1()
{
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", Weight = 25.2 },
              new Package { Company = "Lucerne Publishing", Weight = 18.7 },
              new Package { Company = "Wingtip Toys", Weight = 6.0 },
              new Package { Company = "Adventure Works", Weight = 33.8 } };

    double totalWeight = packages.Sum(pkg => pkg.Weight);

    Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}

/*
 This code produces the following output:

 The total weight of the packages is: 83.7
*/
Structure Package
    Public Company As String
    Public Weight As Double
End Structure

Sub SumEx1()
    ' Create a list of Package values.
    Dim packages As New List(Of Package)(New Package() _
     {New Package With {.Company = "Coho Vineyard", .Weight = 25.2},
      New Package With {.Company = "Lucerne Publishing", .Weight = 18.7},
      New Package With {.Company = "Wingtip Toys", .Weight = 6.0},
      New Package With {.Company = "Adventure Works", .Weight = 33.8}})

    ' Sum the values from each item's Weight property.
    Dim totalWeight As Double = packages.Sum(Function(pkg) _
                                             pkg.Weight)

    ' Display the result.
    Console.WriteLine($"The total weight of the packages is: {totalWeight}")
End Sub

' This code produces the following output:
'
' The total weight of the packages is: 83.7

注解

中的sourcenull项从总和的计算中排除。 如果 source 不包含任何元素或所有元素均为 null,则此方法返回零。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影到数值类型中,特别是在 Nullable<Int64> C# 或 Nullable(Of Int64) Visual Basic 中

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Int32>>)

计算可以为 null 的 Int32 值序列的总和,这些值可通过对输入序列的每个元素调用转换函数获得。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<int> Sum(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<int>> ^ selector);
public static int? Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int?> selector);
static member Sum : seq<'Source> * Func<'Source, Nullable<int>> -> Nullable<int>
<Extension()>
Public Function Sum(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Integer))) As Nullable(Of Integer)

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

用于计算总和的值序列。

selector
Func<TSource,Nullable<Int32>>

应用于每个元素的转换函数。

返回

投影值的总和。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 对序列的投影值求和。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
}

public static void SumEx1()
{
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", Weight = 25.2 },
              new Package { Company = "Lucerne Publishing", Weight = 18.7 },
              new Package { Company = "Wingtip Toys", Weight = 6.0 },
              new Package { Company = "Adventure Works", Weight = 33.8 } };

    double totalWeight = packages.Sum(pkg => pkg.Weight);

    Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}

/*
 This code produces the following output:

 The total weight of the packages is: 83.7
*/
Structure Package
    Public Company As String
    Public Weight As Double
End Structure

Sub SumEx1()
    ' Create a list of Package values.
    Dim packages As New List(Of Package)(New Package() _
     {New Package With {.Company = "Coho Vineyard", .Weight = 25.2},
      New Package With {.Company = "Lucerne Publishing", .Weight = 18.7},
      New Package With {.Company = "Wingtip Toys", .Weight = 6.0},
      New Package With {.Company = "Adventure Works", .Weight = 33.8}})

    ' Sum the values from each item's Weight property.
    Dim totalWeight As Double = packages.Sum(Function(pkg) _
                                             pkg.Weight)

    ' Display the result.
    Console.WriteLine($"The total weight of the packages is: {totalWeight}")
End Sub

' This code produces the following output:
'
' The total weight of the packages is: 83.7

注解

中的sourcenull项从总和的计算中排除。 如果 source 不包含任何元素或所有元素均为 null,则此方法返回零。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影到数值类型中,特别是在 Nullable<Int32> C# 或 Nullable(Of Int32) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Double>>)

计算可以为 null 的 Double 值序列的总和,这些值可通过对输入序列的每个元素调用转换函数获得。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<double> Sum(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<double>> ^ selector);
public static double? Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,double?> selector);
static member Sum : seq<'Source> * Func<'Source, Nullable<double>> -> Nullable<double>
<Extension()>
Public Function Sum(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Double))) As Nullable(Of Double)

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

用于计算总和的值序列。

selector
Func<TSource,Nullable<Double>>

应用于每个元素的转换函数。

返回

投影值的总和。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 对序列的投影值求和。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
}

public static void SumEx1()
{
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", Weight = 25.2 },
              new Package { Company = "Lucerne Publishing", Weight = 18.7 },
              new Package { Company = "Wingtip Toys", Weight = 6.0 },
              new Package { Company = "Adventure Works", Weight = 33.8 } };

    double totalWeight = packages.Sum(pkg => pkg.Weight);

    Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}

/*
 This code produces the following output:

 The total weight of the packages is: 83.7
*/
Structure Package
    Public Company As String
    Public Weight As Double
End Structure

Sub SumEx1()
    ' Create a list of Package values.
    Dim packages As New List(Of Package)(New Package() _
     {New Package With {.Company = "Coho Vineyard", .Weight = 25.2},
      New Package With {.Company = "Lucerne Publishing", .Weight = 18.7},
      New Package With {.Company = "Wingtip Toys", .Weight = 6.0},
      New Package With {.Company = "Adventure Works", .Weight = 33.8}})

    ' Sum the values from each item's Weight property.
    Dim totalWeight As Double = packages.Sum(Function(pkg) _
                                             pkg.Weight)

    ' Display the result.
    Console.WriteLine($"The total weight of the packages is: {totalWeight}")
End Sub

' This code produces the following output:
'
' The total weight of the packages is: 83.7

注解

中的sourcenull项从总和的计算中排除。 如果 source 不包含任何元素或所有元素均为 null,则此方法返回零。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影到数值类型中,特别是在 Nullable<Double> C# 或 Nullable(Of Double) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Single>>)

计算可以为 null 的 Single 值序列的总和,这些值可通过对输入序列的每个元素调用转换函数获得。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<float> Sum(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<float>> ^ selector);
public static float? Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,float?> selector);
static member Sum : seq<'Source> * Func<'Source, Nullable<single>> -> Nullable<single>
<Extension()>
Public Function Sum(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Single))) As Nullable(Of Single)

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

用于计算总和的值序列。

selector
Func<TSource,Nullable<Single>>

应用于每个元素的转换函数。

返回

投影值的总和。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 对序列的投影值求和。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
}

public static void SumEx1()
{
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", Weight = 25.2 },
              new Package { Company = "Lucerne Publishing", Weight = 18.7 },
              new Package { Company = "Wingtip Toys", Weight = 6.0 },
              new Package { Company = "Adventure Works", Weight = 33.8 } };

    double totalWeight = packages.Sum(pkg => pkg.Weight);

    Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}

/*
 This code produces the following output:

 The total weight of the packages is: 83.7
*/
Structure Package
    Public Company As String
    Public Weight As Double
End Structure

Sub SumEx1()
    ' Create a list of Package values.
    Dim packages As New List(Of Package)(New Package() _
     {New Package With {.Company = "Coho Vineyard", .Weight = 25.2},
      New Package With {.Company = "Lucerne Publishing", .Weight = 18.7},
      New Package With {.Company = "Wingtip Toys", .Weight = 6.0},
      New Package With {.Company = "Adventure Works", .Weight = 33.8}})

    ' Sum the values from each item's Weight property.
    Dim totalWeight As Double = packages.Sum(Function(pkg) _
                                             pkg.Weight)

    ' Display the result.
    Console.WriteLine($"The total weight of the packages is: {totalWeight}")
End Sub

' This code produces the following output:
'
' The total weight of the packages is: 83.7

注解

中的sourcenull项从总和的计算中排除。 如果 source 不包含任何元素或所有元素均为 null,则此方法返回零。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影到数值类型中,特别是在 Nullable<Single> C# 或 Nullable(Of Single) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int64>)

计算 Int64 值序列的总和,这些值可通过对输入序列中的每个元素调用转换函数获得。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static long Sum(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, long> ^ selector);
public static long Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,long> selector);
static member Sum : seq<'Source> * Func<'Source, int64> -> int64
<Extension()>
Public Function Sum(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Long)) As Long

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

用于计算总和的值序列。

selector
Func<TSource,Int64>

应用于每个元素的转换函数。

返回

投影值的总和。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 对序列的投影值求和。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
}

public static void SumEx1()
{
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", Weight = 25.2 },
              new Package { Company = "Lucerne Publishing", Weight = 18.7 },
              new Package { Company = "Wingtip Toys", Weight = 6.0 },
              new Package { Company = "Adventure Works", Weight = 33.8 } };

    double totalWeight = packages.Sum(pkg => pkg.Weight);

    Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}

/*
 This code produces the following output:

 The total weight of the packages is: 83.7
*/
Structure Package
    Public Company As String
    Public Weight As Double
End Structure

Sub SumEx1()
    ' Create a list of Package values.
    Dim packages As New List(Of Package)(New Package() _
     {New Package With {.Company = "Coho Vineyard", .Weight = 25.2},
      New Package With {.Company = "Lucerne Publishing", .Weight = 18.7},
      New Package With {.Company = "Wingtip Toys", .Weight = 6.0},
      New Package With {.Company = "Adventure Works", .Weight = 33.8}})

    ' Sum the values from each item's Weight property.
    Dim totalWeight As Double = packages.Sum(Function(pkg) _
                                             pkg.Weight)

    ' Display the result.
    Console.WriteLine($"The total weight of the packages is: {totalWeight}")
End Sub

' This code produces the following output:
'
' The total weight of the packages is: 83.7

注解

如果 source 不包含任何元素,则此方法返回零。

如果提供将 的成员source投影为数值类型的函数 selector,则可以将此方法应用于任意值序列,具体来说就是 Int64

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Int32>)

计算 Int32 值序列的总和,这些值可通过对输入序列中的每个元素调用转换函数获得。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static int Sum(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, int> ^ selector);
public static int Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,int> selector);
static member Sum : seq<'Source> * Func<'Source, int> -> int
<Extension()>
Public Function Sum(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Integer)) As Integer

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

用于计算总和的值序列。

selector
Func<TSource,Int32>

应用于每个元素的转换函数。

返回

投影值的总和。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 对序列的投影值求和。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
}

public static void SumEx1()
{
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", Weight = 25.2 },
              new Package { Company = "Lucerne Publishing", Weight = 18.7 },
              new Package { Company = "Wingtip Toys", Weight = 6.0 },
              new Package { Company = "Adventure Works", Weight = 33.8 } };

    double totalWeight = packages.Sum(pkg => pkg.Weight);

    Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}

/*
 This code produces the following output:

 The total weight of the packages is: 83.7
*/
Structure Package
    Public Company As String
    Public Weight As Double
End Structure

Sub SumEx1()
    ' Create a list of Package values.
    Dim packages As New List(Of Package)(New Package() _
     {New Package With {.Company = "Coho Vineyard", .Weight = 25.2},
      New Package With {.Company = "Lucerne Publishing", .Weight = 18.7},
      New Package With {.Company = "Wingtip Toys", .Weight = 6.0},
      New Package With {.Company = "Adventure Works", .Weight = 33.8}})

    ' Sum the values from each item's Weight property.
    Dim totalWeight As Double = packages.Sum(Function(pkg) _
                                             pkg.Weight)

    ' Display the result.
    Console.WriteLine($"The total weight of the packages is: {totalWeight}")
End Sub

' This code produces the following output:
'
' The total weight of the packages is: 83.7

注解

如果 source 不包含任何元素,则此方法返回零。

如果提供将 的成员source投影为数值类型的函数 selector,则可以将此方法应用于任意值序列,具体来说就是 Int32

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>)

计算 Double 值序列的总和,这些值可通过对输入序列中的每个元素调用转换函数获得。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static double Sum(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, double> ^ selector);
public static double Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,double> selector);
static member Sum : seq<'Source> * Func<'Source, double> -> double
<Extension()>
Public Function Sum(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Double)) As Double

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

用于计算总和的值序列。

selector
Func<TSource,Double>

应用于每个元素的转换函数。

返回

投影值的总和。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 对序列的投影值求和。

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
}

public static void SumEx1()
{
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", Weight = 25.2 },
              new Package { Company = "Lucerne Publishing", Weight = 18.7 },
              new Package { Company = "Wingtip Toys", Weight = 6.0 },
              new Package { Company = "Adventure Works", Weight = 33.8 } };

    double totalWeight = packages.Sum(pkg => pkg.Weight);

    Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}

/*
 This code produces the following output:

 The total weight of the packages is: 83.7
*/
Structure Package
    Public Company As String
    Public Weight As Double
End Structure

Sub SumEx1()
    ' Create a list of Package values.
    Dim packages As New List(Of Package)(New Package() _
     {New Package With {.Company = "Coho Vineyard", .Weight = 25.2},
      New Package With {.Company = "Lucerne Publishing", .Weight = 18.7},
      New Package With {.Company = "Wingtip Toys", .Weight = 6.0},
      New Package With {.Company = "Adventure Works", .Weight = 33.8}})

    ' Sum the values from each item's Weight property.
    Dim totalWeight As Double = packages.Sum(Function(pkg) _
                                             pkg.Weight)

    ' Display the result.
    Console.WriteLine($"The total weight of the packages is: {totalWeight}")
End Sub

' This code produces the following output:
'
' The total weight of the packages is: 83.7

注解

如果 source 不包含任何元素,则此方法返回零。

如果提供将 的成员source投影为数值类型的函数 selector,则可以将此方法应用于任意值序列,具体来说就是 Double

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Decimal>)

计算 Decimal 值序列的总和,这些值可通过对输入序列中的每个元素调用转换函数获得。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static System::Decimal Sum(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, System::Decimal> ^ selector);
public static decimal Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,decimal> selector);
static member Sum : seq<'Source> * Func<'Source, decimal> -> decimal
<Extension()>
Public Function Sum(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Decimal)) As Decimal

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

用于计算总和的值序列。

selector
Func<TSource,Decimal>

应用于每个元素的转换函数。

返回

投影值的总和。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 对序列的投影值求和。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
}

public static void SumEx1()
{
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", Weight = 25.2 },
              new Package { Company = "Lucerne Publishing", Weight = 18.7 },
              new Package { Company = "Wingtip Toys", Weight = 6.0 },
              new Package { Company = "Adventure Works", Weight = 33.8 } };

    double totalWeight = packages.Sum(pkg => pkg.Weight);

    Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}

/*
 This code produces the following output:

 The total weight of the packages is: 83.7
*/
Structure Package
    Public Company As String
    Public Weight As Double
End Structure

Sub SumEx1()
    ' Create a list of Package values.
    Dim packages As New List(Of Package)(New Package() _
     {New Package With {.Company = "Coho Vineyard", .Weight = 25.2},
      New Package With {.Company = "Lucerne Publishing", .Weight = 18.7},
      New Package With {.Company = "Wingtip Toys", .Weight = 6.0},
      New Package With {.Company = "Adventure Works", .Weight = 33.8}})

    ' Sum the values from each item's Weight property.
    Dim totalWeight As Double = packages.Sum(Function(pkg) _
                                             pkg.Weight)

    ' Display the result.
    Console.WriteLine($"The total weight of the packages is: {totalWeight}")
End Sub

' This code produces the following output:
'
' The total weight of the packages is: 83.7

注解

如果 source 不包含任何元素,则此方法返回零。

如果提供将 的成员source投影为数值类型的函数 selector,则可以将此方法应用于任意值序列,具体来说就是 Decimal

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于

Sum<TSource>(IEnumerable<TSource>, Func<TSource,Nullable<Decimal>>)

计算可以为 null 的 Decimal 值序列的总和,这些值可通过对输入序列的每个元素调用转换函数获得。

public:
generic <typename TSource>
[System::Runtime::CompilerServices::Extension]
 static Nullable<System::Decimal> Sum(System::Collections::Generic::IEnumerable<TSource> ^ source, Func<TSource, Nullable<System::Decimal>> ^ selector);
public static decimal? Sum<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,decimal?> selector);
static member Sum : seq<'Source> * Func<'Source, Nullable<decimal>> -> Nullable<decimal>
<Extension()>
Public Function Sum(Of TSource) (source As IEnumerable(Of TSource), selector As Func(Of TSource, Nullable(Of Decimal))) As Nullable(Of Decimal)

类型参数

TSource

source 的元素类型。

参数

source
IEnumerable<TSource>

用于计算总和的值序列。

selector
Func<TSource,Nullable<Decimal>>

应用于每个元素的转换函数。

返回

投影值的总和。

例外

sourceselectornull

示例

下面的代码示例演示如何使用 Sum<TSource>(IEnumerable<TSource>, Func<TSource,Double>) 对序列的投影值求和。

注意

此代码示例使用 方法的重载,该方法不同于本文介绍的特定重载。 若要将示例扩展到本文介绍的重载,请更改 函数的 selector 主体。

class Package
{
    public string Company { get; set; }
    public double Weight { get; set; }
}

public static void SumEx1()
{
    List<Package> packages =
        new List<Package>
            { new Package { Company = "Coho Vineyard", Weight = 25.2 },
              new Package { Company = "Lucerne Publishing", Weight = 18.7 },
              new Package { Company = "Wingtip Toys", Weight = 6.0 },
              new Package { Company = "Adventure Works", Weight = 33.8 } };

    double totalWeight = packages.Sum(pkg => pkg.Weight);

    Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}

/*
 This code produces the following output:

 The total weight of the packages is: 83.7
*/
Structure Package
    Public Company As String
    Public Weight As Double
End Structure

Sub SumEx1()
    ' Create a list of Package values.
    Dim packages As New List(Of Package)(New Package() _
     {New Package With {.Company = "Coho Vineyard", .Weight = 25.2},
      New Package With {.Company = "Lucerne Publishing", .Weight = 18.7},
      New Package With {.Company = "Wingtip Toys", .Weight = 6.0},
      New Package With {.Company = "Adventure Works", .Weight = 33.8}})

    ' Sum the values from each item's Weight property.
    Dim totalWeight As Double = packages.Sum(Function(pkg) _
                                             pkg.Weight)

    ' Display the result.
    Console.WriteLine($"The total weight of the packages is: {totalWeight}")
End Sub

' This code produces the following output:
'
' The total weight of the packages is: 83.7

注解

中的sourcenull项从总和的计算中排除。 如果 source 不包含任何元素或所有元素均为 null,则此方法返回零。

如果提供函数 ,可以将此方法应用于任意值序列, selector该函数将 的成员 source 投影到数值类型中,特别是在 Nullable<Decimal> C# 或 Nullable(Of Decimal) Visual Basic 中。

在 Visual Basic 查询表达式语法中, Aggregate Into Sum() 子句转换为 的 Sum调用。

另请参阅

适用于