2 回答

TA貢獻1847條經驗 獲得超11個贊
首先很抱歉來晚了...
但正如 Gert Arnold 所說,這一切都會在記憶中完成。因此 EF 將獲取內存中包含產品評論的整個產品表,這對性能來說可能是災難性的。
您可能想要做的是(2 是更好的方法):
如果您真的想繼續使用您的擴展方法,請將產品和產品評論中所需的屬性投影到虛擬機中,然后進行過濾。
只需使用 sql 為您提供的平均方法

TA貢獻1864條經驗 獲得超6個贊
EF 中的擴展方法沒有問題。該錯誤與錯誤的數據類型有關。所以請嘗試這種方式
public static class Extensions
{
public static decimal CalculateAverageRating(this IAsyncEnumerable<ProductReview> productReviews)
{
// Calculate and return average rating
return averageRatings;
}
}
var products = _context.Products
.Include(pr => pr.ProductReviews)
.AsQueryable().ToAsyncEnumerable();
if(searchParams.Rating != 0)
products = products.Where(p => p.ProductReviews.CalculateAverageRating() == searchParams.Rating);
PS:我沒有檢查上面的代碼
- 2 回答
- 0 關注
- 213 瀏覽
添加回答
舉報