开发者

LINQ: Stop lazy loading or force properties to be loaded

开发者 https://www.devze.com 2022-12-17 03:55 出处:网络
Consider a report in Windows forms which uses Linq to Objects as a datasource. I have an entity named Loan with an association named Customer. The problem is that when I try to access the .Customer p

Consider a report in Windows forms which uses Linq to Objects as a datasource.

I have an entity named Loan with an association named Customer. The problem is that when I try to access the .Customer property in a report, it returns null or 开发者_运维技巧empty string.

I suppose this is because of lazy loading but I'm not sure to be honest. Is there a way to fix this issue?


In your case, closing the data context leads to null values for data that was not retrieved through a join. Use the DataLoadOptions to explicitely tell the context to perform a join:

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

精彩评论

暂无评论...
验证码 换一张
取 消