Linq to Sql 多一关系

发布于 2024-08-07 13:25:24 字数 1153 浏览 6 评论 0原文

我在 SQL 设置中有 2 个视图:

  • PurchaseOrder
  • PurchaseOrderLineItems

这些视图有许多别名列(它们查看/加入的表没有明智地命名......它是第 3 方产品)

我有 2 个类(下面简化)

    class PurchaseOrder
    {
        public string PoNumber { get; set; }
        public string Vendor { get; set; }
        public DateTime DateCreated { get; set; }

        public IEnumerable<PurchaseOrderLineItems> LineItems { get; set; }
    }

并且

    class PurchaseOrderLineItems
    {
       public string PoNumber { get; set; }
       public string Name { get; set; }
       public double Price { get; set; }
    }

我正在使用 Linq到 Sql - 使用 XML 映射文件(在 sqlmetal.exe 的帮助下创建)

我想要做的是使用来自 PurchaseOrderLineItem 视图的记录有效地填充 PurchaseOrder 中的 IEnumerable - 有效地连接

我想使用 POCO 执行此操作的表 - 而不必添加实体集<>到我的班级,最终,我会将我的 ORM 更改为类似 nHibernate (我相信它具有 bag 属性...?)

目前,我有一个存储过程 - sp_getPurchaseOrderLineItems ,它接受 PONumber,然后返回一个列表PurchaseOrderLineItem 对象,然后我将其添加到我的结果集中(这远非理想)

有什么方法可以做我需要的事情吗?那么基本上,PurchaseOrder 上的查询会返回实例内已填充的 LineItems IEnumerable 吗?

值得一提的是,这将永远是只读的,我们永远不会使用它插入/更新数据。

I have 2 views in SQL set up:

  • PurchaseOrder
  • PurchaseOrderLineItems

These have many columns aliased (the tables they view/join are not sensibly named... it's a 3rd party product)

I have 2 classes (simplified below)

    class PurchaseOrder
    {
        public string PoNumber { get; set; }
        public string Vendor { get; set; }
        public DateTime DateCreated { get; set; }

        public IEnumerable<PurchaseOrderLineItems> LineItems { get; set; }
    }

and

    class PurchaseOrderLineItems
    {
       public string PoNumber { get; set; }
       public string Name { get; set; }
       public double Price { get; set; }
    }

I'm using Linq to Sql - with XML mapping file (created with help from sqlmetal.exe)

What I want to do is effectivly populate the IEnumerable in PurchaseOrder with records from the PurchaseOrderLineItem view - effectively joining the tables

I wanted to do this using POCO - without having to add EntitySet<> to my class, as eventually, I will change my ORM to something like nHibernate (which has bag attribute i believe...?)

Currently, I've got a stored procedure - sp_getPurchaseOrderLineItems which takes the PONumber, and then returns a list of PurchaseOrderLineItem objects, that i then add to my result set (this is far, far from ideal)

is there any way I can do what i need? So that basically, a query on PurchaseOrder returns an already populated IEnumerable of LineItems within the instance?

It's worth mentioning that this will only ever be read-only, we'll never be inserting / updating data using this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一袭水袖舞倾城 2024-08-14 13:25:24

您可以扩展您的PurchaseOrder 类来实现OnLoadedMethod:

public partial class PurchaseOrder 
{
    partial void OnLoaded()
    {
        LineItems = FunctionToCall_sp_getPurchaseOrderLineItems_AndBuildSet();
    }
}

这至少会在您获取采购订单时自动获取行项目。

You can extend your PurchaseOrder class to implement the OnLoadedMethod:

public partial class PurchaseOrder 
{
    partial void OnLoaded()
    {
        LineItems = FunctionToCall_sp_getPurchaseOrderLineItems_AndBuildSet();
    }
}

This will at least get the line items automatically when you get your PO.

仙女山的月亮 2024-08-14 13:25:24

这就是n+1问题。 nHibernate 有一个解决方案,称为 join-fetch 查询。它基本上所做的是订单和订单行之间的外连接查询,这将产生两个表的行计数的乘积。

我认为 Linq2SQL 没有解决方案。但您仍然可以使用存储过程来生成 join-fetch 输出,并使用一些 Linq2Objects 代码来区分结果中的唯一订单和订单行。

This is the n+1 problem. nHibernate has a solution for this, which is called join-fetch querying. What it basically does is a outer-join query between order and order-line, which will result in the product of the row counts of the two tables.

I don't think Linq2SQL does have a solution for it. But you can still use your stored procedure to generate the join-fetch output, and have some Linq2Objects code to distinct the unique orders and the order-lines out of the result.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文