Introduction
Include해서 가져오는 데이터의 값을 필터해서 필요한 값만 가져올수 있도록 하는
Source
- public IQueryable<Category> SearchByParam(string CategoryName, string ProductName)
- {
- // 핵심은 Select를 할 때 DB에서 가져올 항목들을 'select new' 안에 명시하고
- // ToList()로 메모리상에 올리고 이를 return하는것
- // Query2와 같이 let을 이용할 수도 있고
- // Query1과 같이 이용하지 않을수도 있는데
- // ((ObjectQuery)Query1).ToTraceString()를 해 보았을때 얻어지는
- // 쿼리는 100% 동일하다.
- var Query1 = (from C in this.ObjectContext.Categories.Include("Products")
- .Where(t => CategoryName == "" || t.CategoryName.Contains(CategoryName))
- select new
- {
- C = C,
- P = C.Products.Where(tt => ProductName == "" || tt.ProductName.Contains(ProductName))
- }).ToList().Select(t => t.C);
- var Query2 = (from C in this.ObjectContext.Categories.Include("Products")
- .Where(t => CategoryName == "" || t.CategoryName.Contains(CategoryName))
- let Product = C.Products.Where(tt => ProductName == "" || tt.ProductName.Contains(ProductName))
- select new
- {
- C = C,
- P = Product
- }).ToList().Select(t => t.C);
- //string SQL = ((ObjectQuery)Query1).ToTraceString(); // 이를 사용하기 위해선 ToList()를 하지 말아야함
- return Query1.AsQueryable();
- }
- }
- EXTAREA><br /><br /></P>