【问题标题】:Lambda Expression Left JoinLambda 表达式左连接
【发布时间】:2012-09-14 04:10:20
【问题描述】:

我正在阅读此http://jcmartialarts.co.uk/CSharp/Using-Lambda-Expressions-To-LEFT-JOIN-Tables/43,但我无法弄清楚如何做我想做的事。我有这样的表达:

Dim myData = db.Tbl_Exercises.Where(Function(x) x.Exercise_Employee_ID)

我的Tbl_Exercise 模型有一个Exercise_Type_ID。我想使用该 ID 从我的 Tbl_Exercise_Type 模型中获取运动描述的类型,该模型有一个 ExType_Desc 属性,这是我想在我的应用程序中使用的(代替 Tbl_Exercise.Exercise_Type_ID)。

我该怎么做?

编辑:

这是我的模型:

Public Class Tbl_Exercise

    <Key()> Public Property Exercise_ID() As Integer
    Public Property Exercise_Employee_ID() As Integer
    Public Property Exercise_Create_Date() As Date
    <ForeignKey("Tbl_Exercise_Type")>
    Public Property Exercise_Type_ID() As Integer
    Public Property Exercise_Duration() As Integer

    Public Overridable Property Tbl_Exercise_Type As Tbl_Exercise_Type

End Class

Public Class Tbl_Exercise_Type


    <Key()> Public Property ExType_ID() As Integer
    Public Property ExType_Desc() As String
    Public Property Exercise_Create_Date() As Date

    Public Overridable Property Tbl_Exercise() As ICollection(Of Tbl_Exercise)

End Class

Public Class ExerciseDbContext
    Inherits DbContext

    Public Property Tbl_Exercises As DbSet(Of Tbl_Exercise)
    Public Property Tbl_Exercise_Types As DbSet(Of Tbl_Exercise_Type)

End Class

谢谢。

【问题讨论】:

  • 那篇文章看起来很清楚。您能否更具体地说明您遇到问题的地方?
  • 好吧,我真的不知道该怎么做。我不明白我提供的链接中给出的语法。另外,我使用的是 VB,而不是 C#。
  • 啊...只要意识到o =&gt; o.OrderId 等同于Function (o) o.OrderId(o, i) =&gt; new {o, i} 等同于Function(o, i) New With {o, i} [稍后翻译更彻底...]

标签: asp.net-mvc vb.net asp.net-mvc-3 lambda


【解决方案1】:

请参阅下面的翻译。


// C#
var OrderItems = Orders.GroupJoin(Items, o => o.OrderId, i => i.OrderId, (o, i) => new {o, i});
' VB.NET
Dim OrderItems = Orders.GroupJoin(Items, Function( o ) o.OrderId, Function ( i ) i.OrderId, Function( o, i ) New With {o, i})

// C#
var Item = OrderItems.SelectMany(oi => oi.i.DefaultIfEmpty(), (o,i) => new {o.o, i});
' VB.NET
Dim Item = OrderItems.SelectMany( Function( oi ) oi.i.DefaultIfEmpty(), Function ( o, i ) New With { o.o, i } )

// C#
var Qty = Item.Select(oi => oi.i.Qty);
' VB.NET
Dim Qty = Item.Select( Function( oi ) oi.i.Qty )

// C#
var Item = OrderItems.SelectMany(oi => oi.i.DefaultIfEmpty(), (o,i) => new {o.o.OrderDate, i.Qty});
' VB.NET
Dim Item = OrderItems.SelectMany( Function( oi ) oi.i.DefaultIfEmpty(), Function( o, i ) New With { o.o.OrderDate, i.Qty } )

完整示例

Class Order

    Private _orderId As Integer
    Public Property OrderId() As Integer
        Get
            Return _orderId
        End Get
        Set(ByVal value As Integer)
            _orderId = value
        End Set
    End Property

End Class

Class Item

    Private _orderId As Integer
    Public Property OrderId() As Integer
        Get
            Return _orderId
        End Get
        Set(ByVal value As Integer)
            _orderId = value
        End Set
    End Property

    Private _qty As Integer
    Public Property Qty() As Integer
        Get
            Return _qty
        End Get
        Set(ByVal value As Integer)
            _qty = value
        End Set
    End Property

End Class

Module Module1

    Sub Main()

        Dim Orders = New Order() { _
            New Order With {.OrderId = 1}, _
            New Order With {.OrderId = 2}, _
            New Order With {.OrderId = 3} _
        }

        Dim Items = New Item() { _
            New Item With {.OrderId = 1, .Qty = 1}, _
            New Item With {.OrderId = 3, .Qty = 1}, _
            New Item With {.OrderId = 4, .Qty = 1} _
        }

        Dim OrderItems = Orders.GroupJoin(Items, Function(o) o.OrderId, Function(i) i.OrderId, Function(o, i) New With {o, i})
        Dim Item = OrderItems.SelectMany(Function(oi) oi.i.DefaultIfEmpty(), Function(o, i) New With {o.o, i})
        Dim Qty = Item.Select(Function(oi) oi.i.Qty)
        ' Dim Item = OrderItems.SelectMany(Function(oi) oi.i.DefaultIfEmpty(), Function(o, i) New With {o.o.OrderDate, i.Qty})
    End Sub

End Module

【讨论】:

  • 谢谢,但我想我遗漏了一些东西。我正在尝试创建我的模型(我刚刚将其添加到问题中),但出现错误。我不知道如何将您的答案应用于我的场景。我试图只使用data.Add(New Object() {i.Tbl_Exercise_Type.ExType_Desc, i.Exercise_Duration})(没有左连接),但我在这条线上遇到了错误。
  • 就像我说的,您需要更加具体。你遇到了什么错误?
  • 错误只是说,“An error occurred while executing the command definition. See the inner exception for details.”我不确定问题是什么,但我认为它在我的模型中。
  • 如果您查看 InnerException 属性,您将获得更多详细信息。就data.Add(...) 而言……您的问题中没有任何内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
相关资源
最近更新 更多