views:

58

answers:

2

Hi,

  • I have a table 'Menus' containing the names of the menus of the restaurant.
  • I also have a table 'Ingredients' containing the ingredients that are available to the menus.

I don't have any trouble getting the values out of the 'Menus' table into an IQueryable class (PropAllMenus)

Public Class PropAllMenus
Private _MenuID As Integer
Public Property MenuID() As Integer
    Get
        Return _MenuID
    End Get
    Set(ByVal value As Integer)
        _MenuID = value
    End Set
End Property

Private _Name As String
Public Property Name() As String
    Get
        Return _Name
    End Get
    Set(ByVal value As String)
        _Name = value
    End Set
End Property

Private _DaypartID As Integer
Public Property DaypartID() As Integer
    Get
        Return _DaypartID
    End Get
    Set(ByVal value As Integer)
        _DaypartID = value
    End Set
End Property

I just use this code in the DBRepository class

Public Function ListAllMenus() As IQueryable(Of PropAllMenus) Implements IDBRepository.ListAllMenus

    Dim result = From p In _db.Menus Select New PropAllMenus With {.MenuID = p.MenuID, .Name = p.Name}
    Return result

End Function

This works fine.

  • Now, i also have a many to many table 'MenuIngredients' containing the Id's of ingredients that belong to a menu.

However, i don't know what steps i should take to:

  1. Query the DB with a LINQ query to get a LIST of MENU NAMES with all their INGREDIENTS
  2. fill up a new IQuerable(of...) class so i can pass it to my view

I tried using a property of type array (that contained the ingredients of a menu) but that didn't seem to work.

A: 

YOu just need to add a partial class to your current Menu class like this:

public partial class Books
    public property IEnumerable< Ingredient > Ingredients
     get

            return me. MenuIngredients.Select( function (mi) mi.Ingredient);
     end get

    end Property

    public sub AddIngredient( i as Ingredient )
    //do the logic here
    End Sub

    public sub AddIngredient( i as Ingredient )
    //do the logic here
    End Sub

    End

This then would include the ingredients in to your menu class

tuanvt
A: 

Thx for your answer, but could you please explain further. I don't understand how i should write the linq to entities query. I have this:

Public Class PropAllMenus

    Private _MenuID As Integer
    Public Property MenuID() As Integer
        Get
            Return _MenuID
        End Get
        Set(ByVal value As Integer)
            _MenuID = value
        End Set
    End Property

    Private _Name As String
    Public Property Name() As String
        Get
            Return _Name
        End Get
        Set(ByVal value As String)
            _Name = value
        End Set
    End Property

    Private _DaypartID As Integer
    Public Property DaypartID() As Integer
        Get
            Return _DaypartID
        End Get
        Set(ByVal value As Integer)
            _DaypartID = value
        End Set
    End Property


    Private _MenuIngredientProp As IQueryable(Of PropAllMenusIngredients)
    Public Property MenuIngredientProp() As IQueryable(Of PropAllMenusIngredients)
        Get
            Return _MenuIngredientProp
        End Get
        Set(ByVal value As IQueryable(Of PropAllMenusIngredients))
            _MenuIngredientProp = value
        End Set
    End Property




End Class

Public Class PropAllMenusIngredients


    Private _MenuIngredient As String
    Public Property MenuIngredient() As String
        Get
            Return _MenuIngredient
        End Get
        Set(ByVal value As String)
            _MenuIngredient = value
        End Set
    End Property


End Class

How should i write the linq query?

Public Function ListMenuIngredients() As IQueryable(Of PropAllMenus) Implements IDBRepository.ListMenuIngredients

    Dim result = From p In _db.MenuIngredients Select New PropAllMenus With {.Name = p.Menus.Name}

    Return result
End Function
kevinius