使用BLL,DAL在gridview中显示外键的值

时间:2012-11-15 11:30:18

标签: asp.net .net gridview

我有3个表:Car_Model,Car_Brand和Car_Type

Car_Model有2个外键到其他2个表。 我想在gridview中显示car_model的所有内容,而不是其他2个表的外键,它们的值。

目前我的DAL中有以下功能:

Private dc As New ModelDataContext

    Public Function selectAll() As List(Of Model)

        Dim result = From p In dc.Models
                     Join a In dc.Brands
                     On p.car_brand Equals a.Car_Brand_Id
                     Join t In dc.Types
                     On p.Car_type Equals t.Car_Type_id
                     Select p

        Return result.ToList
    End Function

以下是我的BLL:

Public Function selectAll() As List(Of Model)

    Return DALm.selectAll

End Function

1 个答案:

答案 0 :(得分:0)

通常关系可以这样映射:

Class Model
    Public Property Brand As Car_Brand
    Public Property Type As Car_Type
    ...
End Class

但是如果你想自己获取它们,你可以创建一个看起来像这样的中间类:

Class Car_ViewModel
    Public Property Car As Model
    Public Property Brand As Car_Brand
    Public Property Type As Car_Type
End Class

然后在你的查询中:

Public Function selectAll() As List(Of Car_ViewModel)

    Dim result = From p In dc.Models
                 Join a In dc.Brands
                 On p.car_brand Equals a.Car_Brand_Id
                 Join t In dc.Types
                 On p.Car_type Equals t.Car_Type_id
                 Select New Car_ViewModel With { .Car = p, .Brand = a, .Type = t }

    Return result.ToList
End Function