如果参数为空,则查询 return 所有记录
query to return all records if parameter is null
我的数据库 table 是:
----------
| Ad |
----------
|Id
|title
|price
|tags
|brand
|model
我必须按 6 个参数搜索 Ad
,即按品牌、型号、标签、标题、minPrice 和 maxPrice。现在,如果 brand
为空,那么它应该选择所有行,否则只选择那些 brand
等于 userDesiredBrand
.
的行
我的函数是:
public async Task<IHttpActionResult> SearchAds(string brand, string model,string tags,string title, int minPrice, int maxPrice){
if(brand != null && model != null && tags != null && title != null && minPrice != null && maxPrice != null){
var ret = from ad in db.Ads
where ad.brand.Equals(brand) && ad.model.Equals(model) && ad.tags.Equals(tags) && ad.title.Equals(title) && ad.price > minPrice && ad.price < maxPrice
select new{
id = ad.Id,
title = ad.title,
//retrieve other attributes.
}
return OK(ret);
}
if(brand != null && model == null && tags != null && title != null && minPrice != null && maxPrice != null){
var ret = from ad in db.Ads
where ad.brand.Equals(brand) && ad.tags.Equals(tags) && ad.title.Equals(title) && ad.price > minPrice && ad.price < maxPrice
select new{
id = ad.Id,
title = ad.title,
//retrieve other attributes.
}
return OK(ret);
}
if(brand != null && model != null && tags == null && title != null && minPrice != null && maxPrice != null){
var ret = from ad in db.Ads
where ad.brand.Equals(brand) && ad.model.Equals(model) && ad.title.Equals(title) && ad.price > minPrice && ad.price < maxPrice
select new{
id = ad.Id,
title = ad.title,
//retrieve other attributes.
}
return OK(ret);
}
//Do I have to write 6 * 6 if statements or is this achievable in one query?
}
Do I have to write 6 * 6 if statements
绝对不是。如果此逻辑无法按预期用于 null
值:
where ad.brand.Equals(brand)
然后只需在该逻辑中添加对 null
的检查:
where (brand == null || ad.brand.Equals(brand))
另一种方法是分阶段构建查询。像这样:
var ads = db.Ads;
if (!string.IsNullOrEmpty(brand))
ads = ads.Where(ad => ad.brand.Equals(brand));
if (!string.IsNullOrEmpty(tags))
ads = ads.Where(ad => ad.tags.Equals(tags));
// etc.
ads = ads.Select(ad => new {
id = ad.Id,
title = ad.title,
//retrieve other attributes.
});
return OK(ads);
也就是说,您可以根据需要链接任意数量的子句,并且实际查询要到稍后才会针对数据库实现。 (直到它被某些东西实际枚举。在这种情况下,准备响应时可能是 WebAPI 框架本身。)
在一个简单的 SQL 语句中,我将使用以下约束来实现您的目标:
SELECT
...
WHERE
(@brand IS NULL OR brand = @brand)
AND
(@model IS NULL OR model = @model)
...
其中@variables 是参数。将其反向转换为 LINQ 可能如下所示:
where (brand == null || ad.brand == brand) &&
(model == null || ad.model == model) && ...
另一种方式,仅用于教育目的(因为出于性能原因我不建议在生产代码中使用它),将一点一点地构建您的查询:
var query = (from ad in db.Ads);
if (brand != null)
query = query.Where(ad => ad.brand == brand);
if (model != null)
query = query.Where(ad => ad.model == model);
....
检查查询中的空值
where ((brand == null || ad.model.Eqauls(model)
如果您选择 T-SQL,您可以将所有内容写在一个查询中(如果需要,还可以嵌入)
开始
声明@brand VARCHAR(max);
SET @brand = 'WhatsUse'
SELECT * 来自广告
WHERE 品牌为 NULL
SELECT * 来自广告
哪里品牌=@品牌
现在 - 因为您要检查价格的 MIN 和 MAX 值,所以您必须使用临时表进行排序,然后使用 SELECT ..... WHERE .....提取行。
干杯
我的数据库 table 是:
----------
| Ad |
----------
|Id
|title
|price
|tags
|brand
|model
我必须按 6 个参数搜索 Ad
,即按品牌、型号、标签、标题、minPrice 和 maxPrice。现在,如果 brand
为空,那么它应该选择所有行,否则只选择那些 brand
等于 userDesiredBrand
.
我的函数是:
public async Task<IHttpActionResult> SearchAds(string brand, string model,string tags,string title, int minPrice, int maxPrice){
if(brand != null && model != null && tags != null && title != null && minPrice != null && maxPrice != null){
var ret = from ad in db.Ads
where ad.brand.Equals(brand) && ad.model.Equals(model) && ad.tags.Equals(tags) && ad.title.Equals(title) && ad.price > minPrice && ad.price < maxPrice
select new{
id = ad.Id,
title = ad.title,
//retrieve other attributes.
}
return OK(ret);
}
if(brand != null && model == null && tags != null && title != null && minPrice != null && maxPrice != null){
var ret = from ad in db.Ads
where ad.brand.Equals(brand) && ad.tags.Equals(tags) && ad.title.Equals(title) && ad.price > minPrice && ad.price < maxPrice
select new{
id = ad.Id,
title = ad.title,
//retrieve other attributes.
}
return OK(ret);
}
if(brand != null && model != null && tags == null && title != null && minPrice != null && maxPrice != null){
var ret = from ad in db.Ads
where ad.brand.Equals(brand) && ad.model.Equals(model) && ad.title.Equals(title) && ad.price > minPrice && ad.price < maxPrice
select new{
id = ad.Id,
title = ad.title,
//retrieve other attributes.
}
return OK(ret);
}
//Do I have to write 6 * 6 if statements or is this achievable in one query?
}
Do I have to write 6 * 6 if statements
绝对不是。如果此逻辑无法按预期用于 null
值:
where ad.brand.Equals(brand)
然后只需在该逻辑中添加对 null
的检查:
where (brand == null || ad.brand.Equals(brand))
另一种方法是分阶段构建查询。像这样:
var ads = db.Ads;
if (!string.IsNullOrEmpty(brand))
ads = ads.Where(ad => ad.brand.Equals(brand));
if (!string.IsNullOrEmpty(tags))
ads = ads.Where(ad => ad.tags.Equals(tags));
// etc.
ads = ads.Select(ad => new {
id = ad.Id,
title = ad.title,
//retrieve other attributes.
});
return OK(ads);
也就是说,您可以根据需要链接任意数量的子句,并且实际查询要到稍后才会针对数据库实现。 (直到它被某些东西实际枚举。在这种情况下,准备响应时可能是 WebAPI 框架本身。)
在一个简单的 SQL 语句中,我将使用以下约束来实现您的目标:
SELECT
...
WHERE
(@brand IS NULL OR brand = @brand)
AND
(@model IS NULL OR model = @model)
...
其中@variables 是参数。将其反向转换为 LINQ 可能如下所示:
where (brand == null || ad.brand == brand) &&
(model == null || ad.model == model) && ...
另一种方式,仅用于教育目的(因为出于性能原因我不建议在生产代码中使用它),将一点一点地构建您的查询:
var query = (from ad in db.Ads);
if (brand != null)
query = query.Where(ad => ad.brand == brand);
if (model != null)
query = query.Where(ad => ad.model == model);
....
检查查询中的空值
where ((brand == null || ad.model.Eqauls(model)
如果您选择 T-SQL,您可以将所有内容写在一个查询中(如果需要,还可以嵌入)
开始 声明@brand VARCHAR(max); SET @brand = 'WhatsUse'
SELECT * 来自广告 WHERE 品牌为 NULL
SELECT * 来自广告 哪里品牌=@品牌
现在 - 因为您要检查价格的 MIN 和 MAX 值,所以您必须使用临时表进行排序,然后使用 SELECT ..... WHERE .....提取行。
干杯