查询返回的所有记录,如果参数为null

查询返回的所有记录,如果参数为null

问题描述:

我的数据库表:查询返回的所有记录,如果参数为null

---------- 
| Ad | 
---------- 
|Id 
|title 
|price 
|tags 
|brand 
|model 

我要搜索Ad由6个参数即,通过品牌,型号,标签,标题,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? 
} 
+0

你可以写如下子句:''where brand == null || ad.brand.Equals(brand)'' –

+0

您还可以使用每个参数的'if'语句分阶段构建查询。 – juharr

我必须写6 * 6的if语句

绝对不是。如果是用于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框架本身。)

+0

我正在考虑同样的事情(附加'Where'链条)...从我身上赞扬。 –

+0

@Cᴏʀʏ:我们的答案似乎是在互相锁定的步骤:) – David

在一个普通的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); 
.... 
+0

感谢真棒回答upvote从我! –

查询空值检查

where ((brand == null || ad.model.Eqauls(model)

如果您选择T-SQL,你可以写在一个查询中的所有内容(并嵌入它,如果你想要的话)

以 开头DECLARE @brand VARCHAR(max); SET @brand = 'WhatsUse'

SELECT * FROM广告 WHERE品牌IS NULL

SELECT * FROM广告 WHERE品牌= @brand

现在 - 因为你会被检查MIN和MAX价格的价值,你必须使用临时表进行排序,然后使用SELECT ..... WHERE .....用于提取行。

干杯