SQLKata 动态 Where 子句

SQLKata Dynamic Where Clause

我正在使用 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 Post here

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

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);

试试这个:

List<Group> groups = new List<Group>
{
    new Group
    {
        Conditions = new List<Condition>
        {
            new Condition {Field = "Group1Field1", Operator = "=", Value="Group1Value1"},
            new Condition {Field = "Group1Field2", Operator = ">", Value="Group1Value2"},
            new Condition {Field = "Group1Field3", Operator = "<", Value="Group1Value3"},
            new Condition {Field = "Group1Field4", Operator = "=", Value="Group1Value4"}
        }
    },
    new Group
    {
        Conditions = new List<Condition>
        {
            new Condition {Field = "Group2Field1", Operator = "=", Value="Group2Value1"},
            new Condition {Field = "Group2Field2", Operator = ">=", Value="Group2Value2"},
            new Condition {Field = "Group2Field3", Operator = "<=", Value="Group2Value3"}
        }
    }
};

var query = new Query("Countries");
foreach (Group group in groups)
    foreach (Condition c in group.Conditions)
        query.OrWhere(c.Field, c.Operator, c.Value);

Console.WriteLine(new SqlServerCompiler().Compile(query).Sql);

输出为:

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

.NET Fiddle

更新:

我检查了 documentation and tests 并没有发现比以下更好的东西。 要获得您需要使用的输出:

foreach (var group in groups)
{
    if (!group.Conditions.Any())
        continue;

    if (group.Conditions.Count == 1)
    {
        var single = group.Conditions.Single();
        query.OrWhereRaw($"([{single.Field}] {single.Operator} ?)", single.Value);
        continue;
    }

    var first = group.Conditions.First();
    var last = group.Conditions.Last();
    var others = group.Conditions.Skip(1).Take(group.Conditions.Count - 2);

    query.OrWhereRaw($"([{first.Field}] {first.Operator} ?", first.Value);
    foreach (var c in others)
        query.OrWhere(c.Field, c.Operator, c.Value);
    query.OrWhereRaw($"[{last.Field}] {last.Operator} ?)", last.Value);
}

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

而不是:

foreach (Group group in groups)
    foreach (Condition c in group.Conditions)
        query.OrWhere(c.Field, c.Operator, c.Value);

.NETFiddle

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

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);