创建用于过滤名称、价格和可用性的端点
Creating an endpoint for filtering name, price and availability
我有以下 [HttpGet]
请求,假设要过滤产品的名称、价格和可用性。
我不知道我哪里弄错了。
[HttpGet]
public async Task<ActionResult<IEnumerable<ProductDTO>>> GetProducts([FromQuery] ProductParams productParams)
{
var queryable = _context.Products.AsQueryable();
if (!string.IsNullOrEmpty(productParams.Name) || !productParams.Price.HasValue || !productParams.IsAvailable.HasValue)
{
queryable = queryable.Where(x => x.Name == productParams.Name || x.Price == productParams.Price ||
x.IsAvailable == productParams.IsAvailable);
}
return await _context.Products
.Select(x => productsDTO(x))
.ToListAsync();
}
HasValue
检查对象是否不为空。你的 if 条件为假,你应该从 HasValue
中删除感叹号,我认为如果你添加 null 检查会更好。
应该是这样的:
if (!string.IsNullOrEmpty(productParams.Name) ||
productParams?.Price.HasValue ||
productParams?.IsAvailable.HasValue)
{
queryable = queryable.Where(x => x.Name == productParams.Name || x.Price == productParams.Price ||
x.IsAvailable == productParams.IsAvailable);
}
您可以像下面这样更改您的代码:
[HttpGet]
public async Task<ActionResult<IEnumerable<ProductDTO>>> GetProducts([FromQuery] ProductParams productParams)
{
var query = _context.Products.AsQueryable();
if (!string.IsNullOrEmpty(productParams.Name))
{
query = query.Where(p => p.Name== productParams.Name);
}
if (productParams.Price.HasValue)
{
query = query.Where(p =>p.Price == productParams.Price);
}
if (productParams.IsAvailable.HasValue)
{
query = query.Where(p => p.IsAvailable == productParams.IsAvailable);
}
return await query.Select(x => productsDTO(x)).ToListAsync();
}
我有以下 [HttpGet]
请求,假设要过滤产品的名称、价格和可用性。
我不知道我哪里弄错了。
[HttpGet]
public async Task<ActionResult<IEnumerable<ProductDTO>>> GetProducts([FromQuery] ProductParams productParams)
{
var queryable = _context.Products.AsQueryable();
if (!string.IsNullOrEmpty(productParams.Name) || !productParams.Price.HasValue || !productParams.IsAvailable.HasValue)
{
queryable = queryable.Where(x => x.Name == productParams.Name || x.Price == productParams.Price ||
x.IsAvailable == productParams.IsAvailable);
}
return await _context.Products
.Select(x => productsDTO(x))
.ToListAsync();
}
HasValue
检查对象是否不为空。你的 if 条件为假,你应该从 HasValue
中删除感叹号,我认为如果你添加 null 检查会更好。
应该是这样的:
if (!string.IsNullOrEmpty(productParams.Name) ||
productParams?.Price.HasValue ||
productParams?.IsAvailable.HasValue)
{
queryable = queryable.Where(x => x.Name == productParams.Name || x.Price == productParams.Price ||
x.IsAvailable == productParams.IsAvailable);
}
您可以像下面这样更改您的代码:
[HttpGet]
public async Task<ActionResult<IEnumerable<ProductDTO>>> GetProducts([FromQuery] ProductParams productParams)
{
var query = _context.Products.AsQueryable();
if (!string.IsNullOrEmpty(productParams.Name))
{
query = query.Where(p => p.Name== productParams.Name);
}
if (productParams.Price.HasValue)
{
query = query.Where(p =>p.Price == productParams.Price);
}
if (productParams.IsAvailable.HasValue)
{
query = query.Where(p => p.IsAvailable == productParams.IsAvailable);
}
return await query.Select(x => productsDTO(x)).ToListAsync();
}