2010-01-24 192 views
1

考慮使用Linq將對象作爲數據源的Windows窗體中的報表。LINQ:停止延遲加載或強制加載屬性

我有一個名爲Loan的實體,名爲Customer。問題是,當我嘗試訪問報告中的.Customer屬性時,它將返回null或空字符串。

我想這是因爲延遲加載,但我不確定是否誠實。有沒有辦法解決這個問題?

回答

2

在你的情況下,關閉數據上下文會導致沒有通過連接檢索到的數據爲空值。使用DataLoadOptions明確告訴上下文執行加入:

using(var yourDataContext = .....) 
{ 
    DataLoadOptions dlo = new DataLoadOptions(); 
    dlo.LoadWith<Loan>(loanRecord => loanRecord.Customers); 
    yourDataContext.LoadOptions = dlo; 
    //write code to retrieve data 
}