2014-06-24 4 views
0

나는이 linq 성명서를 가지고 있으며 심각한 성능 문제에 직면하고 있습니다. 문제는 코드의 중간에있는 .Count() 호출에 있다고 생각합니다.Count() linq 쿼리 내에서 성능 문제를 피하는 방법?

var products = from product in pm_products 
         join price in GetResellers().Where(x => x.Price != 0) on product.Id equals price.PM_Price.Product_Id into productPrices 
         let minprice = productPrices.Min(x => x.Price) 
         let maxprice = productPrices.Max(x => x.Price) 
         let difference = ((double)maxprice - (double)minprice)/(double)minprice * 100 
         let number = productPrices.Count() 
         let success = productPrices.Where(x => x.Status == PriceStatus.OKAY).Count() 
         let unknown = productPrices.Where(x => x.Status == PriceStatus.NONE).Count() 
         let fail = productPrices.Where(x => x.Status == PriceStatus.FAIL).Count() 
         select new Product 
         { 
          PM_Product = product, 
          BestPrice = minprice, 
          WorstPrice = maxprice, 
          Fail = fail, 
          Number = number, 
          Success = success, 
          Unknown = unknown, 
          Difference = difference 
         }; 

Count()가 쿼리를 실행한다는 것을 읽었지만 한 번만 실행하면됩니다. 위 공사를 기반으로 어떻게이를 달성 할 수 있습니까?

+1

이에서 카운트를받을 수 있나요? 쿼리가 훨씬 빨라 집니까? – dasblinkenlight

+0

글쎄, 당신은 이미 적어도 두 번 (min과 max)의'productPrices' join을 모으고 있습니다, 그래서 나는 Count 만이 그것을 책임지고 있음을 의심합니다. –

+0

예. 그것은 1600ms에서 32ms로 변합니다. – Peter

답변

-1

기본 컬렉션을 가져 와서 메모리에서 수행 할 수 있으며 성능 향상에 도움이 될 수 있습니다. 이런 식으로 뭔가 :

let prodPrices = productPrices.Select(pp=>new {pp.Id, pp.Status}) 

후 최종 select에 포함하고 메모리는()``카운트를 제거하면 어떻게됩니까

+0

downvote에 정말 유용한 기여 –