任何人都可以建议一个库通过 lambda 表达式构建原始 sql 代码吗?不要建议 EF 和 Linq2sql,因为它不提供对查询的控制。
像这样的东西。这是我自己库中的代码。但是,如果这种情况有另一个好的解决方案,我不希望支持这一点。
[Fact]
public void TotalTest()
{
var countFld = SqlField<Person>.Count(p => p.LastName);
var select = new SqlSelect<Person>()
.AddFields(p => p.LastName, p => p.Name)
.AddFields<Passport>(p => p.Number)
.AddField(countFld)
.InnerJoin<Person, Passport>((person, passport) => person.Id == passport.PersonId)
.Where(SqlFilter<Passport>.From(p => p.Number).IsNotNull().And(p => p.Number).NotEqualTo("3812-808316"))
.GroupBy(p => p.LastName)
.Having(SqlFilter<Person>.From<int>(countFld).GreaterThan(2))
.OrderBy(p => p.LastName);
var expected =
@"SELECT
pe.LastName, pe.Name, pa.Number, COUNT(pe.LastName)
FROM
Person pe
INNER JOIN
Passport pa ON pe.Id = pa.PersonId
WHERE
pa.Number IS NOT NULL AND pa.Number <> '3812-808316'
GROUP BY
pe.LastName
HAVING
COUNT(pe.LastName) > 2
ORDER BY
pe.LastName";
Assert.Equal(expected, select.CommandText);
}