0

我有 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
4

1 回答 1

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
于 2012-11-15T14:51:16.560 回答