【问题标题】:How do I use the servicestack ormlite JoinSqlBuilder如何使用 servicestack ormlite JoinSqlBuilder
【发布时间】:2015-03-13 23:52:14
【问题描述】:

没有关于这个类的示例或文档。如果我错了,我真的会对任何链接感到高兴!

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

}

【问题讨论】:

    标签: c# ormlite-servicestack


    【解决方案1】:

    您可以找到 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);
    

    【讨论】:

    • 好的,我现在有了 .Select,但我找不到有关此场景的示例:Retrieve customers with Orders。 customer.cs 有一个 List 属性。这可能吗?
    • 我假设你的意思是这样的问题:stackoverflow.com/questions/20563669/…
    • 是的,谢谢。我将在 v4 中尝试一下。并观看您所说的可能多个查询...
    猜你喜欢
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多