using System.Collections.Generic;
public static void Main()
List<Group> groups = new List<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" }
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 (var group in groups)
if (!group.Conditions.Any())
if (group.Conditions.Count == 1)
var single = group.Conditions.Single();
query.OrWhereRaw(string.Format("([{0}] {1} ?)", single.Field, single.Operator), single.Value);
var first = group.Conditions.First();
var last = group.Conditions.Last();
var others = group.Conditions.Skip(1).Take(group.Conditions.Count - 2);
query.OrWhereRaw(string.Format("([{0}] {1} ?", first.Field, first.Operator), first.Value);
foreach (var c in others)
query.OrWhere(c.Field, c.Operator, c.Value);
query.OrWhereRaw(string.Format("[{0}] {1} ?)", last.Field, last.Operator), last.Value);
query.Where("Id", "=", 10);
Console.WriteLine(new SqlServerCompiler().Compile(query).Sql);
public List<Condition> Conditions { get; set; }
public string Field { get; set; }
public string Operator { get; set; }
public object Value { get; set; }