我如何使用servicestack ormlite JoinSqlBuilder
作者:互联网
没有关于该课程的样本或文档.如果我错了,我真的很高兴有任何链接!
我不明白我应该作为下一个参数传递什么以及如何使整个事情作为对SqlConnection的查询执行.
有人可以帮我吗?
var sql = new JoinSqlBuilder<Schoolyear, Period>()
.Join<Schoolyear, Period>(s => s.Id, p => p.SchoolyearId);
.Where(p => p.MyDate > DateTime.Now) // This Where clause does not work
// How to execute the sql?
// How to attach the query to an SqlConnection?
更新
2小时后确定:
using (IDbConnection con = dbFactory.OpenDbConnection())
{
var sql = new JoinSqlBuilder<Schoolyear, Period>().Join<Schoolyear, Period>(s => s.Id, p => p.SchoolyearId,
destinationWhere: p => p.LessonDate >= startDateOfWeek && p.LessonDate < endDateOfWeek).ToSql();
return con.Query(sql); /* There is not Query on the idbconnection although ormlite is using the con.Query in its samples? or return dbConn.Exec(dbCmd => dbCmd.Select<T>()); There is no .Select extension method? */
}
解决方法:
您可以找到JoinBuilder in the unit tests here的一些很好的示例.要运行联接查询,您需要将构建器转换为SQL,然后将其传递给Select,如下所示:
var sql = jn.ToSql();
var items = con.Select<SchoolYearPeriod>(sql);
您还可以将联接构建器与表达式visitor一起使用,因此可以在联接之后创建复杂的WHERE过滤器,如下所示:
SqlExpressionVisitor<SchoolYearPeriod> ev = Db.CreateExpression<SchoolYearPeriod>();
ev.SelectExpression = join.ToSql();
ev.Where(syp => syp.MyDate > DateTime.Now);
标签:ormlite-servicestack,c 来源: https://codeday.me/bug/20191030/1965061.html