如何在 SqlKata 中将多个 WHERE 子句连接在一起?
How to join multiple WHERE clauses together in SqlKata?
我正在使用 SqlKata 创建动态 SQL 查询。我有一个条件列表,存储在我的数据库中,它们是根据我的业务规则生成的。这是我的代码示例:
var list = new List<Query>();
foreach(var rule in rules){
var q = new Query()
.Where(x=> x.Where("Price", "<", rule.Price).OrWhere("GoodsType", "=", rule.Type));
list.Add(q);
}
现在我想将此列表项连接在一起,但是 none 的 Where() 扩展重载接受 Query
类型参数。有没有办法将 where 子句连接在一起?
这是我需要生成的预期查询的一小部分。
select * from ship_schedule where Path = @path and scheduleDate= @Date
AND (FD.IssueType ='O' OR fd.Path!='ILMTOP' OR (fd.Path='ILMTOP' AND F.carrier !='MAL'))
AND (FD.IssueType ='O' OR fd.Path!='TOPILM' OR (fd.Path='ILMTOP' AND F.carrier !='MAL'))
我需要创建查询的第二行到最后。
Where
方法是可加的,多次调用会在查询中添加多个条件,不需要自己构建条件列表。
var query = new Query("ship_schedule").Where("Path", path);
foreach(var rule in rules) {
// loop over rules and append them to the query
if(col == null) {
query.WhereNull(col);
} else {
query.Where(q =>
q.Where("Price", "<", rule.Price)
.OrWhere("GoodsType", "=", rule.Type)
)
}
}
其他方式
使用When
方法
query.When(condition, q => q.Where(...));
使用WhereIf
方法
query.WhereIf(condition, "Id", "=", 10);
我正在使用 SqlKata 创建动态 SQL 查询。我有一个条件列表,存储在我的数据库中,它们是根据我的业务规则生成的。这是我的代码示例:
var list = new List<Query>();
foreach(var rule in rules){
var q = new Query()
.Where(x=> x.Where("Price", "<", rule.Price).OrWhere("GoodsType", "=", rule.Type));
list.Add(q);
}
现在我想将此列表项连接在一起,但是 none 的 Where() 扩展重载接受 Query
类型参数。有没有办法将 where 子句连接在一起?
这是我需要生成的预期查询的一小部分。
select * from ship_schedule where Path = @path and scheduleDate= @Date
AND (FD.IssueType ='O' OR fd.Path!='ILMTOP' OR (fd.Path='ILMTOP' AND F.carrier !='MAL'))
AND (FD.IssueType ='O' OR fd.Path!='TOPILM' OR (fd.Path='ILMTOP' AND F.carrier !='MAL'))
我需要创建查询的第二行到最后。
Where
方法是可加的,多次调用会在查询中添加多个条件,不需要自己构建条件列表。
var query = new Query("ship_schedule").Where("Path", path);
foreach(var rule in rules) {
// loop over rules and append them to the query
if(col == null) {
query.WhereNull(col);
} else {
query.Where(q =>
q.Where("Price", "<", rule.Price)
.OrWhere("GoodsType", "=", rule.Type)
)
}
}
其他方式
使用When
方法
query.When(condition, q => q.Where(...));
使用WhereIf
方法
query.WhereIf(condition, "Id", "=", 10);