LINQ中基于外表的选择查询

发布于 2024-10-19 14:14:03 字数 865 浏览 1 评论 0原文

我的 LINQ to SQL dbml 文件中有 2 个表、OrderDetails 和 Requests 。 OrderDetailsID 是请求表中的外键。

我想编写查询以根据 OrderId 从 OrderDetails 获取 UnitCost 总和。 如果请求表中每个 OrderDetailsID 都有一行,并且 Requests.Last.RequestType="Refund" 我想从主金额中减少总退款金额,否则如果没有基于 OrderDetailsID 的行,则添加到总和中。

这是我实现该方法的方法。 我希望防止使用“对于每个”。

有什么解决方案吗?

 iRefund = (From od1 In dc.OrderDetails _
            Where od1.OrderID =1  _
            Select od1.UnitCost).Sum

 Dim objOrderDetails = (From od1 In dc.OrderDetails _
                       Where od1.OrderID =1  _
                       Select od1)

 For Each OrderDetail As ORM.Entities.OrderDetail In objOrderDetails
     If Not OrderDetail.Requests Is Nothing Then
         IF OrderDetail.Requests.Last.RequestType="Refund" Then
                   iRefund -= OrderDetail.UnitCost
         End If
     End If  
  Next

I have 2 Tables , OrderDetails and Requests In my LINQ to SQL dbml file.
OrderDetailsID is a foreign key in Requests Table.

I want to write the query to get the sum of UnitCost from OrderDetails based on OrderId.
And If there is a row in Requests Table for each OrderDetailsID, and the Requests.Last.RequestType="Refund" I want to reduce the total refund amount from the main sum otherwise If there is no row based on OrderDetailsID, add to sum.

Here is the way I implement that.
I am looking to prevent using "For each".

Any Solutions?

 iRefund = (From od1 In dc.OrderDetails _
            Where od1.OrderID =1  _
            Select od1.UnitCost).Sum

 Dim objOrderDetails = (From od1 In dc.OrderDetails _
                       Where od1.OrderID =1  _
                       Select od1)

 For Each OrderDetail As ORM.Entities.OrderDetail In objOrderDetails
     If Not OrderDetail.Requests Is Nothing Then
         IF OrderDetail.Requests.Last.RequestType="Refund" Then
                   iRefund -= OrderDetail.UnitCost
         End If
     End If  
  Next

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

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

发布评论

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

评论(1

清晰传感 2024-10-26 14:14:03

如果您想使用 Linq 而不是 for-each 循环,您可以使用 List 的 ForEach 扩展方法:

Dim objOrderDetails = (From od1 In dc.OrderDetails _
                       Where od1.OrderId = 1 _
                       Select od1).ToList()

objOrderDetails.ForEach(AddressOf DecrementAmount)

Private Sub DecrementAmount(ByVal d As OrderDetail)
    If d.Requests IsNot Nothing _
        AndAlso d.Requests.Count > 0 _
        AndAlso d.Requests.Last.RequestType = "Refund" Then
        iRefund -= d.UnitCost
    End If
End Sub

这假设 iRefund 是一个模块变量。

If you want to use Linq instead of a for-each loop, you could use the ForEach extension method of List:

Dim objOrderDetails = (From od1 In dc.OrderDetails _
                       Where od1.OrderId = 1 _
                       Select od1).ToList()

objOrderDetails.ForEach(AddressOf DecrementAmount)

Private Sub DecrementAmount(ByVal d As OrderDetail)
    If d.Requests IsNot Nothing _
        AndAlso d.Requests.Count > 0 _
        AndAlso d.Requests.Last.RequestType = "Refund" Then
        iRefund -= d.UnitCost
    End If
End Sub

This assumes that iRefund is a module variable.

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