【问题标题】:BLToolkit equivalent of LoadWith from L2SBLToolkit 等效于 L2S 中的 LoadWith
【发布时间】:2012-01-28 00:04:03
【问题描述】:

使用 Linq to SQL 时,您可以使用 DataLoadOptions 指定要加载的“子”对象。 BLToolkit 有没有类似的技术?

很高兴使用 BLT 我可以直接创建 BO,例如:

from p in db.Parent
select new Parent
{
  ParentId = p.ParentId,
  Child = p.Child
};

不管怎样走这条路,在创建整个 Child 对象时,我需要指定 Parent 中的每个字段(即 ParentId、ParentName、ParentDob 等)

谢谢。

【问题讨论】:

    标签: linq bltoolkit


    【解决方案1】:

    不完全像 LoadWith,但在我看来,下面的方法更好/更干净(更少魔法)。在您的 BO 中创建一个如下所示的静态函数:

    public static Parent Build(Parent parent, Child child)
    {
      parent.Child = child;
      return parent; 
    }
    

    现在您需要像这样编写 LINQ 查询:

    var query = from p in db.Parent
                select Parent.Build(p, p.Child);
    

    因此,我们让静态函数返回“p”,而不是“select p”或“select new Parent()”,但在返回之前将 Child 对象分配给“parent.Child”。只要您的关联设置正确(BLToolkit.Mapping.Association),p.Child 也会告诉 BLT 加入 Child 表。你可以走得更远,例如 p.Child.Friends.etc。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 1970-01-01
      • 2021-05-26
      • 2017-09-07
      • 2016-10-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多