I have an error displaying products on the main page of my store, where filters such as brand and other items are possible.
All parts of my service work properly. But I have an error in the part where I want to filter the product group. Briefly PG
The group table is the interface table of the other two tables. I have also tried this starting from the group table, but what I have seen is the repetition of each product several times.
public Tuple<List<ShowProductBoxListViewModel>, int>
GetProductList(int pageId = 1, string filter = "", List<int> brandId = null,
string getType = "all", List<int> tWidth = null, List<int> rDiameter = null,
List<int> PG = null, string orderByType = "date", int startPrice = 0,
int endPrice = 0, List<int> selectedGroups = null, int take = 0)
{
if (take == 0)
{
take = 8;
}
IQueryable<Product> result = _context.Products
.Include(c => c.Groups)
.ThenInclude(c => c.ProductGroup);
if (!string.IsNullOrEmpty(filter))
{
result = result.Where(p => p.Title.Contains(filter) || p.Brand.Title.Contains(filter));
}
if (brandId.Count != 0)
{
result = result.Where(c => brandId.Contains(c.BrandId));
}
switch (getType)
{
case "all":
var test = result.Count();
break;
case "discountPrice":
var now = DateTime.Now;
result = result.Where(c => c.PurchasePrice != null
&& c.StartDiscountTime.HasValue
&& c.StartDiscountTime < now
&& c.EndDiscountTime.HasValue
&& c.EndDiscountTime > now);
break;
}
if (tWidth.Count != 0)
{
result = result.Where(c => tWidth.Contains(c.TireWidthId));
}
if (rDiameter.Count != 0)
{
result = result.Where(c => rDiameter.Contains(c.TireDiameterId));
}
if (PG.Count != 0)
{
//result = result.Where(c => PG.Contains(c.id.GroupId));
}
if (startPrice > 0)
{
result = result.Where(p => p.Price > startPrice);
}
if (endPrice > 0)
{
result = result.Where(p => p.Price < endPrice);
}
if (getType == "all")
{
switch (orderByType)
{
case "date":
{
result = result.OrderByDescending
(p => p.CreateDate);
break;
}
case "lowPrice":
{
result = result.OrderBy(p => p.Price);
break;
}
case "highPrice":
{
result = result.OrderByDescending(p => p.Price);
break;
}
}
}
else if (getType == "discountPrice")
{
switch (orderByType)
{
case "date":
{
if (result != null)
{
result = result.OrderByDescending(p => p.CreateDate);
var test = result.Count();
}
break;
}
case "lowPrice":
{
if (result != null)
{
result = result.OrderBy(p => p.PurchasePrice);
}
break;
}
case "highPrice":
{
if (result != null)
{
result = result.OrderByDescending(p => p.PurchasePrice);
}
break;
}
}
}
int skip = (pageId - 1) * take;
int pageCount;
if (result != null)
{
pageCount = result.Count() / take +1;
}
else
{
pageCount = 0;
}
List<ShowProductBoxListViewModel> query = null;
if (result != null)
{
query = result.Select(p => new ShowProductBoxListViewModel()
{
ProductId = p.ProductId,
ProductImage = p.ProductImageName,
Priceoffer = p.PurchasePrice,
Price = p.Price,
ProductTitle = p.Title,
ProductUrl = p.Url,
BrandName = p.Brand.Name,
BrandLogo = p.Brand.ImageName,
ProductDeep = p.ProductDeep,
WeightCode = p.WeightCode.Code,
WeightTitle = p.WeightCode.Title,
SpeedCode = p.SpeedCode.Code,
SpeedTitle = p.SpeedCode.Title,
RingDiameter = p.TireDiameter.Code,
BrandId = p.BrandId,
ProductWidth = p.TireWidth.Code,
EndDiscountTme = p.EndDiscountTime,
ProductCreateDate = p.ProductCreateDate,
StockProduct = p.Stock
}).Skip(skip).Take(take).ToList();
}
return Tuple.Create(query, pageCount);
}
I say again for easier access: My error is in the following section:
result = result.Where (c => PG.Contains (c.Group.GoupId));
I think I do not have access to group properties at all for the relationship I have written. image was added. Image For Access the properties of the last table in ef core in many to many relationships If models were also needed. I will put it in the next update.
PG can be null too and Id can not contain GroupId
if (PG!=null && PG.Count > 0)
{
result = result.Where(c => c.Groups.Any ( g=> PG.Contains(g.GroupId));
}