LINQ 规范化数据

发布于 2024-08-31 20:09:27 字数 879 浏览 7 评论 0原文

我使用的 OMS 在数据库中每条记录最多存储三个行项目。

以下是包含五个行项目的订单示例。

Order Header
Order Detail
   Prod 1
   Prod 2
   Prod 3
Order Detail
   Prod 4
   Prod 5

一条订单标题记录和两条明细记录。

我的目标是为详细记录建立一对一的关系(即每个行项目一个详细记录)。过去,我使用UNION ALL SQL 语句来提取数据。使用 LINQ 有更好的方法来解决这个问题吗?

下面是我第一次尝试使用 LINQ。任何反馈、意见或建议将不胜感激。据我所知,UNION 语句会对流程造成负担?

var orderdetail =
    (from o in context.ORDERSUBHEADs
        select new { 
            edpNo = o.EDPNOS_001, price = o.EXTPRICES_001, 
            qty = o.ITEMQTYS_001 }
    ).Union(from o in context.ORDERSUBHEADs
        select new { edpNo = o.EDPNOS_002, price = o.EXTPRICES_002, 
            qty = o.ITEMQTYS_002 }
    ).Union(from o in context.ORDERSUBHEADs
        select new { edpNo = o.EDPNOS_003, price = o.EXTPRICES_003, 
            qty = o.ITEMQTYS_003 });

I am using an OMS that stores up to three line items per record in the database.

Below is an example of an order containing five line items.

Order Header
Order Detail
   Prod 1
   Prod 2
   Prod 3
Order Detail
   Prod 4
   Prod 5

One order header record and two detail records.

My goal is have a one to one relation for details records(i.e., one detail record per line item). In the past, I used an UNION ALL SQL statement to extract the data. Is there a better approach to this problem using LINQ?

Below is my first attempt at using LINQ. Any feedback, suggestions or recommendations would greatly be appreciated. For what I have read, an UNION statement can tax the process?

var orderdetail =
    (from o in context.ORDERSUBHEADs
        select new { 
            edpNo = o.EDPNOS_001, price = o.EXTPRICES_001, 
            qty = o.ITEMQTYS_001 }
    ).Union(from o in context.ORDERSUBHEADs
        select new { edpNo = o.EDPNOS_002, price = o.EXTPRICES_002, 
            qty = o.ITEMQTYS_002 }
    ).Union(from o in context.ORDERSUBHEADs
        select new { edpNo = o.EDPNOS_003, price = o.EXTPRICES_003, 
            qty = o.ITEMQTYS_003 });

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

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

发布评论

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

评论(1

往日 2024-09-07 20:09:27

Id 构建一个简单的子类

class Record 
{
    public object SubHeading { get; set; }
    public int EdpNo { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}

然后你只需循环它所有

var orders = context.ORDERSUBHEADs.Select(o => 
   new Record[] {
      new Record { SubHeading = o, EdpNo = o.EDPNOS_001, Price = o.EXTPRICES_001, Quantity = o.ITEMQTYS_001 },
      new Record { SubHeading = o, EdpNo = o.EDPNOS_002, Price = o.EXTPRICES_002, Quantity = o.ITEMQTYS_002 },
      new Record { SubHeading = o, EdpNo = o.EDPNOS_003, Price = o.EXTPRICES_003, Quantity = o.ITEMQTYS_003 }
   }
);

IEnumerable allOrders = IEnumerable.Empty;
foreach(Record[] r in orders)
    allOrders = allOrders.Concat(r);

IEnumerable allRecords = allOrders.Cast<Record>();

但坦率地说你的 o 结构很糟糕,你应该将项目列表构建为 IEnumerable 或 IList 或其他东西,而不是#Parameters * #Rows

Id build a simple sub-class

class Record 
{
    public object SubHeading { get; set; }
    public int EdpNo { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }
}

Then you just loop over it all

var orders = context.ORDERSUBHEADs.Select(o => 
   new Record[] {
      new Record { SubHeading = o, EdpNo = o.EDPNOS_001, Price = o.EXTPRICES_001, Quantity = o.ITEMQTYS_001 },
      new Record { SubHeading = o, EdpNo = o.EDPNOS_002, Price = o.EXTPRICES_002, Quantity = o.ITEMQTYS_002 },
      new Record { SubHeading = o, EdpNo = o.EDPNOS_003, Price = o.EXTPRICES_003, Quantity = o.ITEMQTYS_003 }
   }
);

IEnumerable allOrders = IEnumerable.Empty;
foreach(Record[] r in orders)
    allOrders = allOrders.Concat(r);

IEnumerable allRecords = allOrders.Cast<Record>();

But frankly your o structure sucks, you should build the list of items as an IEnumerable or IList or something, not #Parameters * #Rows.

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