1

又是我 :D

我正在学习 LINQ,有一些我想做却做不到的事情。请不要建议其他不是 Linq 的解决方案。谢谢。

    Dim dtSong As DataTable = New DataTable("Songs")
    dtSong.Columns.Add("ID", GetType(Guid))
    dtSong.Columns.Add("Name", GetType(String))
    dtSong.Columns.Add("Type", GetType(String))
    dtSong.Columns.Add("AlbumID", GetType(Guid))

    Dim dtAlbum As DataTable = New DataTable("Album")
    dtAlbum.Columns.Add("ID", GetType(Guid))
    dtAlbum.Columns.Add("Name", GetType(String))

    Dim idsong, idalbum As Guid
    idalbum = Guid.NewGuid()
    dtAlbum.Rows.Add(idalbum, "Best of")

    idsong = Guid.NewGuid()
    dtSong.Rows.Add(idsong, "You are my sunshine", "Pop", idalbum)
    idsong = Guid.NewGuid()
    dtSong.Rows.Add(idsong, "twinkle twinkle", "Pop", idalbum)
    idsong = Guid.NewGuid()
    dtSong.Rows.Add(idsong, "you are my heart", "Rock", idalbum)        


    Dim query = From x In dtSong Join y In dtAlbum
                On x.Field(Of Guid)("AlbumID") Equals
                y.Field(Of Guid)("ID")
                Where (x.Field(Of String)("Type") = "Pop")
                Select New With
                     {
                        .Name = x.Field(Of String)("Name"),
                        .Type = x.Field(Of String)("Type"),
                        .Album = y.Field(Of String)("Name")
                    }

    Dim dtnew As DataTable = query.CopyToDataTable

我想我的 LINQ 查询有问题。我不允许在此查询中使用 CopyToDataTable。我可以使用 For Each 循环打印出结果,但我想要一个新的 DataTable。谢谢您的帮助。抱歉英语不好。

大号

4

0 回答 0