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

时间:2016-01-20 18:18:20

标签: c# sql-server asp.net-mvc ado.net

我的数据库表是:

----------
|   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?
}

4 个答案:

答案 0 :(得分:3)

  

我是否必须写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框架本身。)

答案 1 :(得分:3)

在一个简单的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);
....

答案 2 :(得分:0)

检查查询中的空值

chmod(UPLOAD_DIR . $name, 0777);

答案 3 :(得分:0)

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

首先 DECLARE @brand VARCHAR(max); SET @brand ='WhatsUse'

SELECT * FROM Ad 品牌IS NULL

SELECT * FROM Ad 品牌= @brand

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

干杯

相关问题