本文整理汇总了C#中System.Linq.Queryable.Sum方法的典型用法代码示例。如果您正苦于以下问题:C# Queryable.Sum方法的具体用法?C# Queryable.Sum怎么用?C# Queryable.Sum使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。
在下文中一共展示了Queryable.Sum方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1:
List<float> numbers = new List<float> { 43.68F, 1.25F, 583.7F, 6.5F };
float sum = numbers.AsQueryable().Sum();
Console.WriteLine("The sum of the numbers is {0}.", sum);
输出:
The sum of the numbers is 635.13.
示例2:
float?[] points = { null, 0, 92.83F, null, 100.0F, 37.46F, 81.1F };
float? sum = points.AsQueryable().Sum();
Console.WriteLine("Total points earned: {0}", sum);
输出:
Total points earned: 311.39
示例3: SumEx3
class Package
{
public string Company { get; set; }
public double Weight { get; set; }
}
public static void SumEx3()
{
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 } };
// Calculate the sum of all package weights.
double totalWeight = packages.AsQueryable().Sum(pkg => pkg.Weight);
Console.WriteLine("The total weight of the packages is: {0}", totalWeight);
}
输出:
The total weight of the packages is: 83.7
示例4: Queryable.Sum()
//引入命名空间
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Reflection;
public class MainClass{
public static void Main(){
int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
var query = numbers.Sum();
}
}
示例5: Queryable.Sum(projection)
//引入命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
public class MainClass {
public static void Main() {
string[] words = { "cherry", "apple", "blueberry" };
double totalChars = words.Sum(w => w.Length);
Console.WriteLine("There are a total of {0} characters in these words.", totalChars);
}
}