Lambda Expression Left Join

I was reading this http://jcmartialarts.co.uk/CSharp/Using-Lambda-Expressions-To-LEFT-JOIN-Tables/43 but I cannot figure out how to do what I want. I have this expression:

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

My Tbl_Exercise model has an Exercise_Type_ID. I want to use that ID to fetch the type of exercise description from my Tbl_Exercise_Type model which has an ExType_Desc property which is what I want to use in my application (in place of Tbl_Exercise.Exercise_Type_ID).

How can I do this?

Edit:

This is my model:

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

Thanks.

Answers


See the translations below.


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

Complete Sample

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

Need Your Help

Entity Framework 4.3. Invalid column name 'CreatedOn'

asp.net-mvc entity-framework entity-framework-4

I'm developing an ASP.NET MVC 4 application using VS 2010 and EF 4.3. It retrieves some data from an external database and all worked as expected until I tried to recompile it one day. After the

Debugging JVM crashes under Windows

java windows debugging native

I wrote a piece of software in Java that communicates with USB hardware by using a DLL provided by the hardware vendor. I used the Java Native Access library to write the wrappers to native code.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.