我使用SQLKata在C#中构建了一个SQL语句,并在Github论坛上找到了一些代码,但是它没有编译。我需要帮助让它编译。
我在这行query.Where(q =>上收到两个错误
编译错误(第17行,col 16):并非所有代码路径都返回类型为“System.Func”的lambda表达式中的值 编译错误(第17行,col 16):无法将lambda表达式转换为“object”类型,因为它不是委托类型
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是这里
Github后这里
更新我已经根据亚历克斯的答案更新了小提琴。我在寻找一个子句,因此预期的输出将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);发布于 2019-06-07 15:38:55
我更新了代码以修复错误。
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);发布于 2019-05-26 10:19:03
试试这个:
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更新:
我检查了文档和测试,没有发现比下面更好的东西。要获得所需的输出,请使用:
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);https://stackoverflow.com/questions/56309041
复制相似问题