블로그 이미지
따시쿵

calendar

1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Notice

2015. 8. 10. 16:21 C# with LINQ to SQL

Eager loading?


질문의 문 안에서 쿼리문이 동시에 실행되어 데이타를 가져오는 절차입니다.


예제는 앞 포스팅에서 있는 예제(lazy loading) 를 변경하는 것으로 하겠습니다.


1. Eager loading 을 구현하는 방법 

  • DataLoadOption 을 이용해서
  • Projection 을 이용해서

  



2-1. DataLoadOptions 를 이용하는 방법


Program.cs 파일을 열어서 System.Data.Linq 네임 스페이스를 추가하고, 다음의 코드를 dbContext.Log = Console.Out; 다음에 삽입합니다.

                DataLoadOptions loadOptions = new DataLoadOptions();
                loadOptions.LoadWith<categories>(d => d.Products);
                dbContext.LoadOptions = loadOptions;

전체적인 코드를 아래와 같습니다.

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

namespace MyLINQ11
{
    class Program
    {
        static void Main(string[] args)
        {
            using (NorhwindDBDataContext dbContext = new NorhwindDBDataContext())
            {
                dbContext.Log = Console.Out;

                DataLoadOptions loadOptions = new DataLoadOptions();
                loadOptions.LoadWith<categories>(d => d.Products);
                dbContext.LoadOptions = loadOptions;

                foreach (Categories categories in dbContext.Categories)
                {
                    Console.WriteLine("Category Name = {0}",categories.CategoryName);
                    foreach (Products products in categories.Products)
                    {
                        Console.WriteLine("\t" + products.ProductName);                        
                    }
                    Console.WriteLine();
                }
            }

            Console.ReadKey();
        }
    }
}



2-2. Projection 를 이용하는 방법


Program.cs 파일을 열어서 다음의 코드를 dbContext.Log = Console.Out; 다음에 삽입합니다.

        var linqQuery = from categories in dbContext.Categories
                             select new { CategoryName = categories.CategoryName, Products = categories.Products };

전체적인 코드를 아래와 같습니다.

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

namespace MyLINQ11
{
    class Program
    {
        static void Main(string[] args)
        {
            using (NorhwindDBDataContext dbContext = new NorhwindDBDataContext())
            {
                dbContext.Log = Console.Out;

                var linqQuery = from categories in dbContext.Categories
                                select new { CategoryName = categories.CategoryName, Products = categories.Products };

                foreach (var categories in linqQuery)
                {
                    Console.WriteLine("Category Name = {0}",categories.CategoryName);
                    foreach (Products products in categories.Products)
                    {
                        Console.WriteLine("\t" + products.ProductName);                        
                    }
                    Console.WriteLine();
                }
            }

            Console.ReadKey();
        }
    }
}


3. 결과 화면입니다.



결과 화면에서 보듯이, 조인 쿼리를 한다는 것입니다. 

그래서 앞의 lazy loading 시와 비교해서 Categories 테이블의 테이타가 루프를 돌면서 하나씩 데이타를 가져와서 관련된 Products 테이블의 해당하는 제품을 가져오는 반면에, eager loading 은 Categories 테이블과 Products 테이블을 조인해서 한번에 데이타를 가져오는 방법입니다.


소스 파일 : 

MyLINQ11.zip



posted by 따시쿵