我如何使用servicestack ormlite JoinSqlBuilder

时间:2013-12-17 09:56:46

标签: c# ormlite-servicestack

此课程没有样本或文档。如果我错了,我会对任何链接感到高兴!

我不明白我应该将什么作为下一个参数传递,以及如何使整个事件在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? */

}

1 个答案:

答案 0 :(得分:4)

您可以找到一些JoinBuilder in the unit tests here的好例子。要运行连接查询,需要将构建器转换为SQL,然后将其传递给Select,如下所示:

var sql = jn.ToSql();
var items = con.Select<SchoolYearPeriod>(sql);

您还可以将连接构建器与表达式访问者结合使用,这样您就可以在连接后创建复杂的WHERE过滤器,如下所示:

SqlExpressionVisitor<SchoolYearPeriod> ev = Db.CreateExpression<SchoolYearPeriod>();
ev.SelectExpression = join.ToSql();
ev.Where(syp => syp.MyDate > DateTime.Now);