Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更聪明的where条款?_C#_Linq_Linq To Sql - Fatal编程技术网

C# 更聪明的where条款?

C# 更聪明的where条款?,c#,linq,linq-to-sql,C#,Linq,Linq To Sql,我最近一直在试验LINQtoSQL,有一个快速的问题。 基本前提是我有一个搜索请求,其中包含一个Make和Model,我用它来搜索包含cars的DB my Where子句的表达式如下所示: .Where(c => c.make == search.make && c.model == search.model) 当我的搜索同时包含make和model时,这很好。当它只包含make(反之亦然)而不是两个搜索字段时,就会出现问题。我想让它归还所有该品牌的汽车,但它一辆也不归

我最近一直在试验LINQtoSQL,有一个快速的问题。 基本前提是我有一个搜索请求,其中包含一个Make和Model,我用它来搜索包含cars的DB

my Where子句的表达式如下所示:

.Where(c => c.make == search.make  && c.model == search.model)
当我的搜索同时包含make和model时,这很好。当它只包含make(反之亦然)而不是两个搜索字段时,就会出现问题。我想让它归还所有该品牌的汽车,但它一辆也不归还

我假设这是因为它正在寻找make加上一个null或空的模型

除了用一系列“if not null append to query”类型的步骤手动建立查询之外,还有什么优雅的方法可以解决这个问题吗?

您是否尝试过:

.Where(c => (search.make == null || c.make == search.make) && 
            (search.model == null || c.model == search.model))
.Where(c => (search.make == null || c.make == search.make)  && (search.model == null || c.model == search.model))
更新:这里有一个对一般问题的很好的处理和干净的解决方案: . 大家一致认为扩展方法是最干净的。

您是否尝试过:

.Where(c => (search.make == null || c.make == search.make)  && (search.model == null || c.model == search.model))
更新:这里有一个对一般问题的很好的处理和干净的解决方案: . 大家一致认为扩展方法是最干净的。

这应该是可行的

.Where(c => 
    (search.make == null || c.make == search.make) &&
    (search.model == null || c.model == search.model))
这应该行得通

.Where(c => 
    (search.make == null || c.make == search.make) &&
    (search.model == null || c.model == search.model))

你可以这样写:

.Where(c => c.make == search.make ?? c.make && c.model == search.model ?? c.model)

你可以这样写:

.Where(c => c.make == search.make ?? c.make && c.model == search.model ?? c.model)
假设属性是字符串

Where(c => (search.make == null || c.make == search.make) && 
           (search.model == null || c.model == search.model))
假设属性是字符串。

IMO,拆分它:

Where(c => (search.make == null || c.make == search.make) && 
           (search.model == null || c.model == search.model))
IQueryable<Car> query = ...
if(!string.IsNullOrEmpty(search.make))
    query = query.Where(c => c.make == search.make);
if(!string.IsNullOrEmpty(search.model))
    query = query.Where(c => c.model== search.model);
IQueryable查询=。。。
如果(!string.IsNullOrEmpty(search.make))
query=query.Where(c=>c.make==search.make);
如果(!string.IsNullOrEmpty(search.model))
query=query.Where(c=>c.model==search.model);
这会产生最合适的TSQL,因为它不会包含冗余的
WHERE
子句或附加参数,从而允许RDBMS(单独)优化“make”、“model”和“make and model”查询。

IMO,拆分它:

IQueryable<Car> query = ...
if(!string.IsNullOrEmpty(search.make))
    query = query.Where(c => c.make == search.make);
if(!string.IsNullOrEmpty(search.model))
    query = query.Where(c => c.model== search.model);
IQueryable查询=。。。
如果(!string.IsNullOrEmpty(search.make))
query=query.Where(c=>c.make==search.make);
如果(!string.IsNullOrEmpty(search.model))
query=query.Where(c=>c.model==search.model);

这会产生最合适的TSQL,因为它不会包含冗余的
WHERE
子句或附加参数,从而允许RDBMS(单独)优化“make”、“model”和“make and model”查询。

这不是做错了吗?(获取模型为空的汽车,即使搜索的是特定品牌和型号?)谢谢@Tao,我误解了这个问题。相应地更正了我的答案。@Boob:如果它起作用,那就归功于Tao,他一开始就做对了。这不是做错了吗?(获取模型为空的汽车,即使搜索的是特定品牌和型号?)谢谢@Tao,我误解了这个问题。相应地更正了我的答案。@Boob:如果它起作用,那就归功于Tao,他一开始就做对了。这也会引起我的兴趣,现在我用If-Else解决了这种问题,然后在这两种情况下我都执行了不同的Linq-to-Sql查询。这也会引起我的兴趣,现在我用If-Else解决了这类问题,然后在这两种情况下我都执行了不同的Linq-to-Sql查询。取决于您如何创建搜索对象。事实上,在重读问题时,甚至可能不需要调用IsNullOrEmpty。取决于您如何创建搜索对象。谢谢,这很有意义。我知道会有更好的办法,只是没有冲我发火……这是漫长的一周:)谢谢,这很有意义。我知道会有更好的办法,只是没有冲我发火……这是漫长的一周:)谢谢保罗,你能解释一下这里发生了什么吗?我对LINQ和LAMBDA的东西很陌生,以前没有见过“?”语法吗?
是coalesce操作符。如果左侧的值为空,则将使用右侧的值。这相当于
(x==null?y:x)
谢谢保罗,你能解释一下这里发生了什么吗?我对LINQ和LAMBDA的东西很陌生,以前没有见过“?”语法吗?
是coalesce操作符。如果左侧的值为空,则将使用右侧的值。它相当于
(x==null?y:x)