0

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

谢谢。

4

1 回答 1

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
于 2012-09-21T18:28:07.363 回答