2010-01-24 195 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 
}