CSharp · Console
using System;
using SqlKata;
using SqlKata.Compilers;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
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)
{
query.OrWhere(q => {
foreach (Condition c in group.Conditions)
{
q.OrWhere(c.Field, c.Operator, c.Value);
}
return q;
});
}
query.Where("Id", "=", 10);
Console.WriteLine(new SqlServerCompiler().Compile(query).Sql);
}
}
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; }
}