.Net Project/.Net C#

101장 프로젝션

Magic_kit 2009. 8. 20. 11:18
반응형

프로젝션 ?
select절은 출력 대상을 지정하는데 순회 변수를 적어 데이터 소스의 값을 그대로 출력

select절을 변경하면 출력 형태를 바꿀 수 있는데 이처럼 결과셋의 출력 형태를 데이터 소스와는 다르게 변형하는 것을 프로젝션이라고 한다. 

Projection.Cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 프로젝션
{
    class Program
    {
        public class Product
        {
            public string Name { get; set; }
            public int Quantity { get; set; }

        }
        public class ProName
        {
            public string ModelName { get; set; }
        }
        static void Main(string[] args)
        {
            int[] data = { 3, 4, 5, 2, 1 };

             //기본형태,배열형태
            IEnumerable<int> q = from d in data where d % 2 == 0 select d;
            int[] even = (from d in data where d % 2 == 0 select d).ToArray(); 
            
            //리스트               
           List<int> lst = (from d in data where d % 2 == 0 select d).ToList();            

  Product[] Products = {
                                    new Product {Name="닷넷", Quantity=1000},
                                    new Product{Name="닷넷", Quantity=1000}
                               };

            var pro = from p in Products select new Product
                         { Name = p.Name, Quantity = p.Quantity };
           
            foreach (var item in pro)
            {
                Console.WriteLine("{0} {1}", item.Name, item.Quantity);
            }
        }
    }
}

반응형