'LINQ'에 해당되는 글 1건

  1. 2010.09.06 C# LINQ by 아르다

C# LINQ

분류없음 : 2010.09.06 15:59
Tag ,


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

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            List order = new List {
                new Order {Products = "노트북", Company = "삼성", Price = 195},
                new Order {Products = "노트북", Company = "LG", Price = 200},
                new Order {Products = "노트북", Company = "HP", Price = 190},
                new Order {Products = "노트북", Company = "SONY", Price = 220},
                new Order {Products = "노트북", Company = "DELL", Price = 180},
                new Order {Products = "노트북", Company = "삼성", Price = 170},
                new Order {Products = "노트북", Company = "LG", Price = 190},
                new Order {Products = "노트북", Company = "HP", Price = 180},
                new Order {Products = "노트북", Company = "SONY", Price = 240},
                new Order {Products = "노트북", Company = "DELL", Price = 170},
                new Order {Products = "컴퓨터", Company = "삼성", Price = 115},
                new Order {Products = "컴퓨터", Company = "LG", Price = 105},
                new Order {Products = "컴퓨터", Company = "HP", Price = 120},
                new Order {Products = "컴퓨터", Company = "SONY", Price = 130},
                new Order {Products = "컴퓨터", Company = "DELL", Price = 100},
                new Order {Products = "컴퓨터", Company = "삼성", Price = 125},
                new Order {Products = "컴퓨터", Company = "LG", Price = 115},
                new Order {Products = "컴퓨터", Company = "HP", Price = 125},
                new Order {Products = "컴퓨터", Company = "SONY", Price = 135},
                new Order {Products = "컴퓨터", Company = "DELL", Price = 110}
            };

            /* 모두 가져오기 */
            var allSelect = from item in order select item;
            foreach (var item in allSelect)
            {
                Console.WriteLine("제품명 : {0}({1}), 가격 : {2}",
                    item.Products, item.Company, item.Price);
            }

            /* where */
            Console.WriteLine("--= 190보다 낮은 가격의 제품들 =--");
            var where = from item in order where item.Price < 190 select item;
            foreach (var item in where)
            {
                Console.WriteLine("제품명 : {0}({1}), 가격 : {2}",
                    item.Products, item.Company, item.Price);
            }

            /* group */
            var group = from item in order group item by item.Company;
            foreach (var item in group)
            {
                Console.WriteLine("====================");
                Console.WriteLine("  {0} 회사 제품");
                Console.WriteLine("====================");

                foreach (var item2 in item)
                {
                    Console.WriteLine("제품명 : {0}({1}), 가격 : {2}",
                        item2.Products, item2.Company, item2.Price);
                }
                Console.WriteLine();
            }

            /* where 2 */
            Console.WriteLine("--= HP회사의 190보다 낮은 가격 제품들 =--");
            var where2 =
                from item in order where item.Company == "HP" && item.Price < 190 select item;
            foreach (var item in where2)
            {
                Console.WriteLine("제품명 : {0}({1}), 가격 : {2}",
                    item.Products, item.Company, item.Price);
            }

            /* orderby */
            var orderby = 
                from item in order orderby item.Company ascending, item.Price descending select item;
            foreach (var item in orderby)
            {
                Console.WriteLine("제품명 : {0}({1}), 가격 : {2}",
                    item.Products, item.Company, item.Price);
            }

            Console.ReadLine();
        }
    }

    class Order
    {
        public string Products { get; set; }
        public string Company { get; set; }
        public int Price { get; set; }
    }
}
저작자 표시 비영리 동일 조건 변경 허락
신고
Posted by 아르다

댓글을 달아 주세요

티스토리 툴바