2013-12-17 56 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