0

I doing a auto load more product list, with sort by the category name (a-z). Let say each load more is taking 20 of products, starting from Beverage, then Cereals, so I will get 20 of Beverage until Beverage is none, then take from Cereals accordingly.

I tried to use solution , sorting a list from another list.

However getting an error InvalidOperationException: Unable to determine the serialization information for x => Convert(value(MyProject.Services.Catalog.ProductService+<>c__DisplayClass35_0).orderedcategorylist.IndexOf(x.ProductCategories.FirstOrDefault().CategoryId), Object).

        var catbuilder = Builders<Category>.Filter;
        var catfilter = catbuilder.Where(x => x.Enable == true);
        var categorylist = await _categoryRepository.Collection.Find(catfilter).ToListAsync();
        var orderedcategorylist = categorylist.OrderBy(x => x.Name).Select(x => x.Id).ToList();

        var builder = Builders<Product>.Filter;
        var filter = builder.Eq(x => x.Enable, true);
        var query = _productRepository.Collection.Find(filter).SortBy(x => orderedcategorylist.IndexOf(x.ProductCategories.FirstOrDefault().CategoryId)).Limit(take).Skip(skip);
        return await query.ToListAsync();


        public partial class Product 
        {
              public string Name {get; set;}
              public virtual ICollection<ProductCategory> ProductCategories {
                    get { return _productCategories ?? (_productCategories = new List<ProductCategory>()); }
                    protected set { _productCategories = value; }
              }
        }

        public partial class ProductCategory
        {
              public string ProductId { get; set; }
              public string CategoryId { get; set; }
        }

        public partial class Category 
        {
              public string Name { get; set; }
              public string Description { get; set; }
        }
FeelRightz
  • 2,777
  • 2
  • 38
  • 73

0 Answers0