再努力一点点

没有烟抽的日子
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Linq to sql立即加载

Posted on 2010-07-13 14:55  ZhangPeng.Chen  阅读(185)  评论(0)    收藏  举报
有时我们需要立即加载指定的关联数据。
DataLoadOptions loadOptions = new DataLoadOptions();
loadOptions.LoadWith
<Customer>(it => it.Orders);
DataContext.LoadOptions 
= loadOptions;

DataContext.Log = Console.Out; // 用来查看请求的SQL语句,确定只发送一次请求。
LoadWith会将指定关联的数据全部加载,有时我们需要过滤关联数据怎么办?
使用DataLoadOptions的另一个方法AssociateWith<T>(Expression<Func<T, Object>>)。
DataLoadOptions loadOptions = new DataLoadOptions();
loadOptions.LoadWith
<Customer>(it => it.Orders.Where(o=> o.ShippedDate != DateTime.Today));
DataContext.LoadOptions 
= loadOptions;