2013-11-28 105 views
1

我先使用EF5代碼。當我執行我的查詢:EF5:LINQ to Entities無法識別該方法System.Nullable System.Decimal中位數

var priceIndexQuery = from r in unitOfWork.Repository<StockIndexPriceResult>() 
             where request.CalculateModel.StockCompanies.Contains(r.StockCompanyId) 
              && r.StockIndexId == request.CalculateModel.StockIndexId 
              && r.ResultDate >= dateFiveYearsAgo 
             orderby r.ResultDate descending 
             group r by r.ResultDate 
              into g 
              select g; 

然後:

var query = priceIndexQuery.Select(g => new CalculateAggregateModel 
                { 
                 ResultDate = g.Key, 
                 Value = g.Median(p => p.Result ?? 0), 
                 PeriodType = "Day" 
                }); 

            resultData = query.ToList(); 

我上query.ToList錯誤時拋出();到目前爲止我所知道的是MSSQL無法執行中位數函數。我有辦法解決它嗎?

+2

你可以叫'AsEnumerate()'之前,你的選擇。這會強制查詢在此時執行數據庫**,然後在內存中繼續。確保只選擇儘可能多的數據,而不是更多。 – user3038092

回答

1

實體框架無法將您的中值方法轉換爲SQL,因此您需要在執行中值方法之前查詢數據庫。 嘗試.ToList().AsEnumerable()第一個查詢後立即執行:

var priceIndexQuery = (from r in unitOfWork.Repository<StockIndexPriceResult>() 
             where request.CalculateModel.StockCompanies.Contains(r.StockCompanyId) 
              && r.StockIndexId == request.CalculateModel.StockIndexId 
              && r.ResultDate >= dateFiveYearsAgo 
             orderby r.ResultDate descending 
             group r by r.ResultDate 
              into g 
              select g).ToList(); 
相關問題