2013-12-17 64 views
3

没有关于此类的示例或文档。如果我错了,我真的会为任何链接感到高兴!如何使用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? 

UPDATE

OK 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? */ 

} 

回答

4

你可以找到JoinBuilder in the unit tests here的一些很好的例子。要运行的连接,你需要建造者转换为SQL,然后通过它来选择,这样的查询:

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

您还可以使用join建设者结合表达访问者,这样你就可以创建复杂的WHERE加入后的过滤器,如下所示:

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

好的我现在选择,但我找不到有关此方案的示例:使用订单检索客户。 customer.cs具有列表属性。这可能吗? – HelloWorld

+0

我假设你的意思是这样的问题:http://stackoverflow.com/questions/20563669/populating-pocos-with-servicestack-ormlite/20568007#20568007 – hross

+0

是的谢谢。我会在第4版中尝试一下。并观看你所说的多个疑问...... – HelloWorld