gpt4 book ai didi

c# - SQLKata 动态 Where 子句

转载 作者:行者123 更新时间:2023-11-30 16:37:58 26 4
gpt4 key购买 nike

我正在使用 SQLKataC# 中构建一个 SQL 语句,发现 Github 论坛上发布了一些代码,但它无法编译。我需要帮助才能编译它。

我在 query.Where(q =>

这行收到两个错误

Compilation error (line 17, col 16): Not all code paths return a value in lambda expression of type 'System.Func'

Compilation error (line 17, col 16): Cannot convert lambda expression to type 'object' because it is not a delegate type

class Group
{
public List<Condition> Conditions {get; set;}
}

class Condition
{
public string Field {get; set;}
public string Operator {get; set;}
public object Value {get; set;}
}

var query = new Query("Countries");

foreach(Group group in groups)
{
query.Where(q =>
{
foreach(Condition c in group.Conditions)
{
q.OrWhere(c.Field, c.Operator, c.Value);
}
});
}

.NET Fiddle 是 here

Github 帖子 here

更新我已经根据亚历克斯的回答更新了 fiddle 。我正在寻找一个子句,所以预期的输出是用括号括起来的 OR 语句。它现在主要按预期工作除了每个组都应该在它自己的parens内,如下所示:

SELECT * FROM [Countries] WHERE ([Group1Field1] = @p0 OR [Group1Field2] > @p1 OR [Group1Field3] < @p2 OR [Group1Field4] = @p3) OR ([Group2Field1] = @p4 OR [Group2Field2] >= @p5 OR [Group2Field3] <= @p6) AND [Id] = @p7

最终更新弄清楚了。给出上述预期输出。谢谢。

var query = new Query("Countries");

foreach (Group group in groups)
{
query.OrWhere(q => {
foreach (Condition c in group.Conditions)
{
q.OrWhere(c.Field, c.Operator, c.Value);
}
return q;

});

}
query.Where("Id", "=", 10);

最佳答案

我已更新代码以修复错误。

var query = new Query("Countries");

foreach (Group group in groups)
{
query.OrWhere(q => {
foreach (Condition c in group.Conditions)
{
q.OrWhere(c.Field, c.Operator, c.Value);
}
return q;

});

}
query.Where("Id", "=", 10);

关于c# - SQLKata 动态 Where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56309041/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com