在多对多关系中访问 EF Core 中最后一个 table 的属性
Access the properties of the last table in EF Core in many to many relationships
我在商店的主页上显示产品时出错,其中可以使用品牌和其他项目等过滤器。
我服务的所有部分都正常工作。但是我要过滤产品组的部分有错误。简要PG
组table是另外两个table的接口table。我也试过这个从组table开始,但我看到的是每个产品重复几次。
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);
}
为了方便访问,我再说一遍:
我的错误在以下部分:
result = result.Where (c => PG.Contains (c.Group.GoupId));
我认为对于我所写的关系,我根本无权访问组属性。
图片已添加。
Image For Access the properties of the last table in ef core in many to many relationships
如果还需要模型。我会在下次更新时放上去。
PG也可以为null,Id不能包含GroupId
if (PG!=null && PG.Count > 0)
{
result = result.Where(c => c.Groups.Any ( g=> PG.Contains(g.GroupId));
}
我在商店的主页上显示产品时出错,其中可以使用品牌和其他项目等过滤器。
我服务的所有部分都正常工作。但是我要过滤产品组的部分有错误。简要PG
组table是另外两个table的接口table。我也试过这个从组table开始,但我看到的是每个产品重复几次。
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);
}
为了方便访问,我再说一遍: 我的错误在以下部分:
result = result.Where (c => PG.Contains (c.Group.GoupId));
我认为对于我所写的关系,我根本无权访问组属性。 图片已添加。 Image For Access the properties of the last table in ef core in many to many relationships 如果还需要模型。我会在下次更新时放上去。
PG也可以为null,Id不能包含GroupId
if (PG!=null && PG.Count > 0)
{
result = result.Where(c => c.Groups.Any ( g=> PG.Contains(g.GroupId));
}