Hi,
I have a plugin that uses to sort products by stock count,
My plugin works on my local well, but when I installed that on my website I got this error
=================================
Method not found: 'System.Linq.IOrderedQueryable`1<Nop.Core.Domain.Catalog.Product> Nop.Services.Catalog.ProductExtensions.OrderBy(System.Linq.IQueryable`1<Nop.Core.Domain.Catalog.Product>, Nop.Core.Domain.Catalog.ProductSortingEnum)'.
Full message
System.MissingMethodException: Method not found: 'System.Linq.IOrderedQueryable`1<Nop.Core.Domain.Catalog.Product> Nop.Services.Catalog.ProductExtensions.OrderBy(System.Linq.IQueryable`1<Nop.Core.Domain.Catalog.Product>, Nop.Core.Domain.Catalog.ProductSortingEnum)'.
at Nop.Plugin.Misc.OutOfStock.Services.OutOfStockProductService.SearchProductsAsync(Int32 pageIndex, Int32 pageSize, IList`1 categoryIds, IList`1 manufacturerIds, Int32 storeId, Int32 vendorId, Int32 warehouseId, Nullable`1 productType, Boolean visibleIndividuallyOnly, Boolean excludeFeaturedProducts, Nullable`1 priceMin, Nullable`1 priceMax, Int32 productTagId, String keywords, Boolean searchDescriptions, Boolean searchManufacturerPartNumber, Boolean searchSku, Boolean searchProductTags, Int32 languageId, IList`1 filteredSpecOptions, ProductSortingEnum orderBy, Boolean showHidden, Nullable`1 overridePublished)
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)
at Nop.Plugin.Misc.OutOfStock.Services.OutOfStockProductService.SearchProductsAsync(Int32 pageIndex, Int32 pageSize, IList`1 categoryIds, IList`1 manufacturerIds, Int32 storeId, Int32 vendorId, Int32 warehouseId, Nullable`1 productType, Boolean visibleIndividuallyOnly, Boolean excludeFeaturedProducts, Nullable`1 priceMin, Nullable`1 priceMax, Int32 productTagId, String keywords, Boolean searchDescriptions, Boolean searchManufacturerPartNumber, Boolean searchSku, Boolean searchProductTags, Int32 languageId, IList`1 filteredSpecOptions, ProductSortingEnum orderBy, Boolean showHidden, Nullable`1 overridePublished)
at Nop.Web.Factories.CatalogModelFactory.PrepareCategoryProductsModelAsync(Category category, CatalogProductsCommand command)
at Nop.Web.Factories.CatalogModelFactory.PrepareCategoryModelAsync(Category category, CatalogProductsCommand command)
at Nop.Web.Controllers.CatalogController.Category(Int32 categoryId, CatalogProductsCommand command)
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at WebMarkupMin.AspNetCore5.WebMarkupMinMiddlewareBase.Invoke(HttpContext context)
at StackExchange.Profiling.MiniProfilerMiddleware.Invoke(HttpContext context) in C:\projects\dotnet\src\MiniProfiler.AspNetCore\MiniProfilerMiddleware.cs:line 125
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Nop.Services.Authentication.AuthenticationMiddleware.InvokeAsync(HttpContext context)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
at Nop.Services.Installation.InstallUrlMiddleware.InvokeAsync(HttpContext context, IWebHelper webHelper)
at Nop.Services.Common.KeepAliveMiddleware.InvokeAsync(HttpContext context, IWebHelper webHelper)
at Microsoft.AspNetCore.ResponseCompression.ResponseCompressionMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.StatusCodePagesMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)
=====================================
And here is my code in plugin
=====================================
public override async Task<IPagedList<Product>> SearchProductsAsync(
int pageIndex = 0,
int pageSize = int.MaxValue,
IList<int> categoryIds = null,
IList<int> manufacturerIds = null,
int storeId = 0,
int vendorId = 0,
int warehouseId = 0,
ProductType? productType = null,
bool visibleIndividuallyOnly = false,
bool excludeFeaturedProducts = false,
decimal? priceMin = null,
decimal? priceMax = null,
int productTagId = 0,
string keywords = null,
bool searchDescriptions = false,
bool searchManufacturerPartNumber = true,
bool searchSku = true,
bool searchProductTags = false,
int languageId = 0,
IList<SpecificationAttributeOption> filteredSpecOptions = null,
ProductSortingEnum orderBy = ProductSortingEnum.Position,
bool showHidden = false,
bool? overridePublished = null
)
{
//some databases don't support int.MaxValue
if (pageSize == int.MaxValue)
pageSize = int.MaxValue - 1;
var productsQuery = _productRepository.Table;
productsQuery = productsQuery.Where(a => !a.Deleted);
if (!showHidden)
productsQuery = productsQuery.Where(p => p.Published);
else if (overridePublished.HasValue)
productsQuery = productsQuery.Where(p => p.Published == overridePublished.Value);
//apply store mapping constraints
productsQuery = await _storeMappingService.ApplyStoreMapping(productsQuery, storeId);
//HERE IS MY CODE******
var queryString = _contextAccessor.HttpContext.Request.QueryString.ToString().Split("&");
if (queryString.Count() > 1)
{
var stockSplit = queryString.Where(a => a.Contains("stockCount")).SingleOrDefault();
if (stockSplit != null)
{
var stockCount = stockSplit.Split("=")[1];
if (Convert.ToBoolean(stockCount))
{
productsQuery = productsQuery.Where(a => a.StockQuantity > 0);
}
}
}
//***---
//apply ACL constraints
if (!showHidden)
{
var customer = await _workContext.GetCurrentCustomerAsync();
productsQuery = await _aclService.ApplyAcl(productsQuery, customer);
}
if (vendorId > 0)
productsQuery = productsQuery.Where(a => a.VendorId == vendorId);
if (warehouseId > 0)
{
productsQuery = productsQuery.Where(
p => !p.UseMultipleWarehouses ? p.WarehouseId == warehouseId :
_productWarehouseInventoryRepository.Table.Any(pwi => pwi.Id == warehouseId && pwi.ProductId == p.Id));
}
if (productType != null)
productsQuery = productsQuery.Where(p => p.ProductTypeId == (int)productType);
if (showHidden)
productsQuery = productsQuery.Where(p => LinqToDB.Sql.Between(DateTime.UtcNow, p.AvailableStartDateTimeUtc ?? DateTime.MinValue, p.AvailableEndDateTimeUtc ?? DateTime.MaxValue));
if (priceMin > 0 || priceMin != null)
productsQuery = productsQuery.Where(p => p.Price >= priceMin);
if (priceMax > 0 || priceMax != null)
productsQuery = productsQuery.Where(p => p.Price <= priceMax);
if (!string.IsNullOrEmpty(keywords))
{
var langs = await _languageService.GetAllLanguagesAsync(showHidden: true);
//Set a flag which will to points need to search in localized properties. If showHidden doesn't set to true should be at least two published languages.
var searchLocalizedValue = languageId > 0 && langs.Count() >= 2 && (showHidden || langs.Count(l => l.Published) >= 2);
var productsByKeywords =
from p in _productRepository.Table
where p.Name.Contains(keywords) ||
(searchDescriptions &&
(p.ShortDescription.Contains(keywords) || p.FullDescription.Contains(keywords))) ||
(searchManufacturerPartNumber && p.ManufacturerPartNumber == keywords) ||
(searchSku && p.Sku == keywords)
select p.Id;
// search by SKU for ProductAttributeCombination
if (searchSku)
{
productsByKeywords = productsByKeywords.Union(
from pac in _productAttributeCombinationRepository.Table
where pac.Sku == keywords
select pac.ProductId);
}
if (searchProductTags)
{
productsByKeywords = productsByKeywords.Union(
from pptm in _productTagMappingRepository.Table
join pt in _productTagRepository.Table on pptm.ProductTagId equals pt.Id
where pt.Name == keywords
select pptm.ProductId
);
if (searchLocalizedValue)
{
productsByKeywords = productsByKeywords.Union(
from pptm in _productTagMappingRepository.Table
join lp in _localizedPropertyRepository.Table on pptm.ProductTagId equals lp.EntityId
where lp.LocaleKeyGroup == nameof(ProductTag) &&
lp.LocaleKey == nameof(ProductTag.Name) &&
lp.LocaleValue.Contains(keywords)
select lp.EntityId);
}
}
if (searchLocalizedValue)
{
productsByKeywords = productsByKeywords.Union(
from lp in _localizedPropertyRepository.Table
let checkName = lp.LocaleKey == nameof(Product.Name) &&
lp.LocaleValue.Contains(keywords)
let checkShortDesc = searchDescriptions &&
lp.LocaleKey == nameof(Product.ShortDescription) &&
lp.LocaleValue.Contains(keywords)
let checkProductTags = searchProductTags &&
lp.LocaleKeyGroup == nameof(ProductTag) &&
lp.LocaleKey == nameof(ProductTag.Name) &&
lp.LocaleValue.Contains(keywords)
where
(lp.LocaleKeyGroup == nameof(Product) && lp.LanguageId == languageId) && (checkName || checkShortDesc) ||
checkProductTags
select lp.EntityId);
}
productsQuery =
from p in productsQuery
from pbk in LinqToDB.LinqExtensions.InnerJoin(productsByKeywords, pbk => pbk == p.Id)
select p;
}
if (categoryIds is not null)
{
if (categoryIds.Contains(0))
categoryIds.Remove(0);
if (categoryIds.Any())
{
var productCategoryQuery =
from pc in _productCategoryRepository.Table
where (!excludeFeaturedProducts || !pc.IsFeaturedProduct) &&
categoryIds.Contains(pc.CategoryId)
select pc;
productsQuery =
from p in productsQuery
where productCategoryQuery.Any(pc => pc.ProductId == p.Id)
select p;
}
}
if (manufacturerIds is not null)
{
if (manufacturerIds.Contains(0))
manufacturerIds.Remove(0);
if (manufacturerIds.Any())
{
var productManufacturerQuery =
from pm in _productManufacturerRepository.Table
where (!excludeFeaturedProducts || !pm.IsFeaturedProduct) &&
manufacturerIds.Contains(pm.ManufacturerId)
select pm;
productsQuery =
from p in productsQuery
where productManufacturerQuery.Any(pm => pm.ProductId == p.Id)
select p;
}
}
if (productTagId > 0)
{
productsQuery =
from p in productsQuery
join ptm in _productTagMappingRepository.Table on p.Id equals ptm.ProductId
where ptm.ProductTagId == productTagId
select p;
}
if (filteredSpecOptions?.Count > 0)
{
var specificationAttributeIds = filteredSpecOptions
.Select(sao => sao.SpecificationAttributeId)
.Distinct();
foreach (var specificationAttributeId in specificationAttributeIds)
{
var optionIdsBySpecificationAttribute = filteredSpecOptions
.Where(o => o.SpecificationAttributeId == specificationAttributeId)
.Select(o => o.Id);
var productSpecificationQuery =
from psa in _productSpecificationAttributeRepository.Table
where psa.AllowFiltering && optionIdsBySpecificationAttribute.Contains(psa.SpecificationAttributeOptionId)
select psa;
productsQuery =
(from p in productsQuery
where productSpecificationQuery.Any(pc => pc.ProductId == p.Id)
select p).OrderByDescending(a => a.StockQuantity);
}
}
//HERE IS MY CODE**************
var tempproductsQuery = productsQuery.Where(a => a.StockQuantity > 0).ToList();
var qOutOfStock = productsQuery.Where(a => a.StockQuantity == 0).ToList();
if (qOutOfStock.Count() > 0)
{
foreach (var item in qOutOfStock)
{
tempproductsQuery.Add(item);
}
}
productsQuery = tempproductsQuery.AsQueryable();
return await productsQuery.OrderBy(orderBy).ToPagedListAsync(pageIndex, pageSize);
}