LINQ 规范化数据
我使用的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Id 构建一个简单的子类
然后你只需循环它所有
但坦率地说你的 o 结构很糟糕,你应该将项目列表构建为 IEnumerable 或 IList 或其他东西,而不是
#Parameters * #Rows
。Id build a simple sub-class
Then you just loop over it all
But frankly your
o
structure sucks, you should build the list of items as anIEnumerable
orIList
or something, not#Parameters * #Rows
.