SQLKata动态Where子句

时间:2019-05-25 21:56:38

标签: c# lambda sqlkata

我正在使用SQLKataSQL中建立一个C#语句,并在Github论坛上找到了一些代码,但没有编译。我需要帮助使其编译。

此行query.Where(q =>上我收到两个错误

  

编译错误(第17行,第16行):并非所有代码路径都在lambda表达式中返回“ System.Func”类型的值

     

编译错误(第17行,第16行):无法将lambda表达式转换为类型“对象”,因为它不是委托类型

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

更新 我根据亚历克斯的回答更新了小提琴。我正在寻找一个子句,因此预期的输出是将OR语句括在括号中。现在,大多数情况都按预期运行,除了每个组都应位于自己的括号内,如下所示

  

选择*从[国家/地区]在([Group1Field1] = @ p0或[Group1Field2]> @ p1或[Group1Field3] <@ p2或[Group1Field4] = @ p3)或([Group2Field1] = @ p4或[Group2Field2 ]> = @ p5或[Group2Field3] <= @ p6)和[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);

2 个答案:

答案 0 :(得分:1)

尝试一下:

{% for image in images %}

输出为:

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

.NET Fiddle

更新

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

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

代替:

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

.NETFiddle

答案 1 :(得分:0)

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

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