當前位置: 首頁>>代碼示例>>C#>>正文


C# Queryable.Sum方法代碼示例

本文整理匯總了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);
開發者ID:.NET開發者,項目名稱:System.Linq,代碼行數:5,代碼來源:Queryable.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);
開發者ID:.NET開發者,項目名稱:System.Linq,代碼行數:5,代碼來源:Queryable.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);
}
開發者ID:.NET開發者,項目名稱:System.Linq,代碼行數:20,代碼來源:Queryable.Sum

輸出:

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();
   }
}
開發者ID:C#程序員,項目名稱:System.Linq,代碼行數:14,代碼來源:Queryable.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);
    }
}
開發者ID:C#程序員,項目名稱:System.Linq,代碼行數:15,代碼來源:Queryable.Sum


注:本文中的System.Linq.Queryable.Sum方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。