.NET프로그래밍/C# 3.5 SP1

87. LINQ - 합계 카운트 평균

holland14 2009. 8. 19. 16:53

using System;
using System.Linq;

public class 합계카운트평균
{
    public static void Main()
    {
        // Input
        int[] data = { 3, 5, 4, 2, 1 };

        // Process
        var q = from d in data
                where d % 2 == 0
                select d;

        int sum = q.Sum(); // 합계
        int cnt = q.Count(); // 카운트
        int avg = Convert.ToInt32(q.Average()); // 평균
        int max = (from d in data select d).Max(); // 최대값
        int min = (from d in data select d).OrderByDescending(p => p).Last(); // 최소값

        // Output
        Console.WriteLine("합계 : {0}\n카운트 : {1}\n평균 : {2}", sum, cnt, avg);
        Console.WriteLine("최대값 : {0}", max);
        Console.WriteLine("최소값 : {0}", min);
    }

}