Попробуйте это:
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
Обновление:
Я проверил документацию и тесты и не нашел ничего лучше следующего.
Чтобы получить вывод, вам нужно использовать:
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