0

我无法弄清楚这一点。我想传入一个 GenreID 并获取相应类型的对象列表,即如果我传入 MyGenre.RockAndRoll,我想要一个 RockAndRoll 对象列表。

Public Enum MyGenre Alternative = 0 rockAndRoll = 1 country = 2 End Enum

        Public Interface IGenre
            Inherits IDisposable
            Property title As String
            Property artist As String
            Property duration As Decimal
            Property rating As Integer
            Sub Listen()
        End Interface

        Public Class RockAndRoll
            Implements IGenre

            Public Property artist As String Implements IGenre.artist

            Public Property duration As Decimal Implements IGenre.duration

            Public Property rating As Integer Implements IGenre.rating

            Public Property title As String Implements IGenre.title

            Public Sub Listen() Implements IGenre.Listen
                '...
            End Sub
#Region "IDisposable Support"
 '...
#End Region

        End Class

        Public Class Country
            Implements IGenre

            Public Property artist As String Implements IGenre.artist

            Public Property duration As Decimal Implements IGenre.duration

            Public Property rating As Integer Implements IGenre.rating

            Public Property title As String Implements IGenre.title

            Public Sub Listen() Implements IGenre.Listen
                '...
            End Sub
#Region "IDisposable Support"
  '...
#End Region

        End Class


        Public Function GetAlternativeList() As IEnumerable(Of IGenre)
            Return GetIGenreList(MyGenre.alternative)
        End Function

        Public Function GetrockAndRollList() As IEnumerable(Of IGenre)
            Return GetIGenreList(MyGenre.alternative)
        End Function

        Public Function GetIGenreList(p_MyGenre As MyGenre) As IEnumerable(Of IGenre)
            Using db As New OracleDataContext
                Return (From s In db.SongList
                        Where s.Genre= MyGenre _
                        Select CType(Activator.CreateInstance(p_MyGenre.GetGenreType,
                                                               New With {.title = s.title, .artist = p1.artist, etc etc etc}), p_MyGenre.GetGenreTyp)).ToList
            End Using
        End Function


        <Extension()> _
        Private Function GetGenreType(p_formatID As MyGenre) As Type
            Select Case p_formatID
                Case MyGenre.rockAndRoll
                    Return GetType(RockAndRoll)
                Case MyGenre.country
                    Return GetType(PlayawayView)
                Case Else
                    Return Nothing
            End Select
        End Function
#

编辑

#

为了清楚起见,我想添加一个额外的示例。第一个令人困惑。让我们从格式的角度来看。

我想返回一份产品列表(DVD、CD、蓝光等),每种格式都有自己的内部类(用于艺术品尺寸、处理、合法性等原因)。

Class CD -> Implments IProduct
Class DVD -> Implements IProduct
Class BLURAY -> Implements IProduct

i.e. dim MyList = (from p in MyProductTable select p).tolist


returns 4 records. 
(0) 
    title: Motown Unmixed
    artist: Various
    duration: 36:25
    format: CD
    upc: 024543246157
(1)
    title: Classical Bytes - Bach
    artist: Various
    duration: 54:32
    format: CD
    upc: 709387901743
(2)
    title: Star Wars: Episode VI - Return of the Jedi
    artist: null
    duration: 136.00
    format: DVD
    upc: 883928446172
(3)
    title: Perfect Stranger
    artist: null
    duration: 95.36
    format: BLU
    upc: 043215190627

我想要的是一个列表 IPRODUCT。这可以通过点击数据库 3 次(对于每种格式类型)来完成。并将结果附加到 IPRODUCT 列表中。

dim MyProductList as new List(of IProduct) 

MyProductList.addrange((from p in MyProductTable where p.format = "CD" select new CD with {.title = p.title, etc etc etc}).tolist)

MyProductList.addrange((from p in MyProductTable where p.format = "DVD" select new DVD with {.title = p.title, etc etc etc}).tolist)

MyProductList.addrange((from p in MyProductTable where p.format = "BLU" select new BLURAY with {.title = p.title, etc etc etc}).tolist)

我想将上面的 3 个查询合并为一个查询。此查询将返回 CD、DVD 和 BLURAY 对象的列表。

4

1 回答 1

0

我更简单的方法是放弃界面并使用流派类型进行枚举,然后您只需要一个类,您可以在其中选择每个类的流派。

Dim query = db.Songlist.Where(Function(s) s.genre = genre).ToList
于 2013-07-26T15:15:18.533 回答