C#

83.C# - 표준쿼리문 ( 무명메서드, 람다식 )

Godffs 2009. 8. 19. 21:15
반응형
표준쿼리문으로 무명메서드, 람다식으로 호출하는 예제입니다.

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;

public class 표준쿼리연산자
{
   public static bool EvenNum(int x) { return (x % 2 == 0); }

    public static void Main()
    {
        //Input
        int[] data = { 3, 5, 4, 2, 1 };

        //Process
        var q =
            //from d in data select d; //전체 출력
            //from d in data where d % 2 == 0 orderby d select d;[1]쿼리식
            //from d in data where d % 2 == 0 select d; //[2]짝수만 출력
            //data.Where( delegate(int x)  //[3] 무명메서드
                                        { return (x % 2 == 0); }
                               );

            data.Where(x => x % 2 == 0); //[4] 람다식
   
        //Output
        foreach (var item in q)
        {
            Console.WriteLine("{0}", item);
        }
    }
}
반응형