holland14 2009. 8. 19. 14:32

using System;
using System.Linq;
using System.Collections.Generic; //

public class Product
{
    public string Name { get; set; }    // 상품명
    public int UnitPrice { get; set; }  // 단가
}

public class 쿼리식
{
    public static void Main()
    {
        //[1] 원본 데이터 : Product형 배열
        Product[] pros = {
                             new Product{Name="닷넷", UnitPrice=1000},
                             new Product{Name="자바", UnitPrice=900}
                         };
       
        Object[] arr = {10, 20, 30};

        //[2] 쿼리식(LINQ)으로 뽑아내기 : LINQ(.NET 통합 언어 쿼리)
        IEnumerable<Product> q = from p in pros
                                 where p.Name[0] == '닷' // 조건절(if문 같은 역할)
                                 select p;

        IEnumerable<int> query = from int a in arr 
                                 where a % 4 == 0   // 4의 배수인 것만
                                 select a;

        //[3] 출력
        foreach (var item in q)
        {
            Console.WriteLine("{0}, {1}", item.Name, item.UnitPrice);
        }

        foreach (var item in query)
        {
            Console.WriteLine("{0}", item);
        }
    }
}

 

/*
Google에서
" .NET 통합 언어 쿼리 " 로 검색해서(MSDN에 있음)
자주 꼭 읽어볼 것!(필독!!!)
*/