본문 바로가기

Study/C#

LINQ ( 병걸이형 )

LINQ는 .NET에서만

 

http://www.tutorialspoint.com/asp.net/asp.net_linq.htm

 

실습

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

namespace SeoulUnivCon
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Books> books = new List<Books>();
            books = Books.GetBooks();

            // 쿼리식
            /*
            var temp = from b in books
                       where b.Price >= 500 &&
                       b.Title.StartsWith("P")
                       select b;
            */


            //var temp = books.Where(book => book.Price >= 500 && book.Title.StartsWith("P"));
            var temp = books.Max(book=>book.Price);

            //foreach (var item in temp)
            //{
            //    Console.WriteLine("Title = {0}", item.Title);
            //}

            Console.WriteLine("{0}", temp);
        }
    }
}

public class Books
{
    public string ID { get; set; }
    public string Title { get; set; }
    public decimal Price { get; set; }
    public DateTime DateOfRelease { get; set; }

    public static List<Books> GetBooks()
    {
        List<Books> list = new List<Books>();
        list.Add(new Books
        {
            ID = "001",
            Title = "Programming in C#",
            Price = 634.76m,
            DateOfRelease = Convert.ToDateTime("2010-02-05")
        });

        list.Add(new Books
        {
            ID = "002",
            Title = "Learn Jave in 30 days",
            Price = 250.76m,
            DateOfRelease = Convert.ToDateTime("2011-08-15")
        });

        list.Add(new Books
        {
            ID = "003",
            Title = "Programming in ASP.Net 4.0",
            Price = 700.00m,
            DateOfRelease = Convert.ToDateTime("2011-02-05")
        });

        list.Add(new Books
        {
            ID = "004",
            Title = "VB.Net Made Easy",
            Price = 500.99m,
            DateOfRelease = Convert.ToDateTime("2011-12-31")
        });

        list.Add(new Books
        {
            ID = "005",
            Title = "Programming in C",
            Price = 314.76m,
            DateOfRelease = Convert.ToDateTime("2010-02-05")
        });

        list.Add(new Books
        {
            ID = "006",
            Title = "Programming in C++",
            Price = 456.76m,
            DateOfRelease = Convert.ToDateTime("2010-02-05")
        });

        list.Add(new Books
        {
            ID = "007",
            Title = "Datebase Developement",
            Price = 1000.76m,
            DateOfRelease = Convert.ToDateTime("2010-02-05")
        });
        return list;
    }

}

'Study > C#' 카테고리의 다른 글

MVVMlight  (0) 2012.10.07
LINQ ( 링크 )  (0) 2012.10.07
Media APIs  (0) 2012.10.06
Code Snippets  (0) 2012.10.06
12. 09. 22  (0) 2012.09.22