Lambda表达式左连接

时间:2012-09-21 17:49:08

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

我正在阅读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

感谢。

1 个答案:

答案 0 :(得分:2)

请参阅以下翻译。


// 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