Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

Linq result to object

Dim League As List(Of Game) = New List(Of Game)
FillLeagueList(league)
...
now I want the following
Dim HomeList as VariantType
HomeList = (From c In League Where c.GetGameNumber = (dayNumber - 1) Select c.GetAwayTeam)

But I want that HomeList is declared as List(Of Team). So I need to cast the linq-result into this list. Can someone help me

i tried this:
HomeList = (From c In League Where c.GetGameNumber = (dayNumber - 1) Select c.GetAwayTeam, c.GetHomeTeam, c.GetGameNumber).Cast(Of Team).ToList()
HomeList = (From c In League Where c.GetGameNumber = (dayNumber - 1) Select c.GetAwayTeam).Cast(Of Team).ToList()

Public Class Game
    Private _HomeTeam As String
    Private _AwayTeam As String
    Private _Game As Integer

    Public Sub New(ByVal homeTeam As String, ByVal awayTeam As String, ByVal game As Integer)
        _HomeTeam = HomeTeam
        _AwayTeam = AwayTeam
        _Game = game

    End Sub

    Public Property GetHomeTeam()
        Get
            Return _HomeTeam
        End Get
        Set(ByVal value)

        End Set
    End Property

    Public Property GetAwayTeam()
        Get
            Return _AwayTeam
        End Get
        Set(ByVal value)

        End Set
    End Property

    Public Property GetGameNumber()
        Get
            Return _Game
        End Get
        Set(ByVal value)

        End Set
    End Property
End Class

Open in new window

0
Dhaest
Asked:
Dhaest
  • 3
  • 2
  • 2
2 Solutions
 
Wayne Taylor (webtubbs)Commented:
HomeList, based on your LINQ, will return an IEnumerable(Of String), not IEnumerable(Of Game), because of the "SELECT" statement.

If you want to return List(Of Game), you should do this....

        Dim HomeList As List(Of Game)
        HomeList = (From c In League Where c.GetGameNumber = (dayNumber - 1)).ToList

Wayne
0
 
DhaestAuthor Commented:
It's not a gamelist that I want back, but a teamlist that played at home previous daynumber

' this list will be filled and will contain games.
Dim League As List(Of Game) = New List(Of Game)

' Now I need a list of teams that played home at previous round
Dim HomeList as VariantType
HomeList = (From c In League Where c.GetGameNumber = (dayNumber - 1) Select c.GetAwayTeam)

-- this code gives what I want (only it's a string-list and not a list<team>), so I need a way to convert this list<string> into a list<team>
Public Class Team
    Private name As String

    Public Sub New(ByVal TeamName As String)
        name = TeamName
    End Sub

    Public Property GetTeam()
        Get
            Return name
        End Get
        Set(ByVal value)

        End Set
    End Property

End Class

Open in new window

0
 
Wayne Taylor (webtubbs)Commented:
How about declaring the 'GetAwayTeam' property in the Game class as Team? Then you can simply do this....

        Dim HomeList As List(Of Team)
        HomeList = (From c In League Where c.GetGameNumber = (dayNumber - 1) Select c.GetAwayTeam).ToList

Wayne
Public Class Team
    Private name As String

    Public Sub New(ByVal TeamName As String)
        name = TeamName
    End Sub

    Public Property GetTeam() As String
        Get
            Return name
        End Get
        Set(ByVal value As String)

        End Set
    End Property

End Class

Public Class Game
    Private _HomeTeam As Team
    Private _AwayTeam As Team
    Private _Game As Integer

    Public Sub New(ByVal homeTeam As String, ByVal awayTeam As String, ByVal game As Integer)
        _HomeTeam = New Team(homeTeam)
        _AwayTeam = New Team(awayTeam)
        _Game = game
    End Sub

    Public Property GetHomeTeam() As Team
        Get
            Return _HomeTeam
        End Get
        Set(ByVal value As Team)

        End Set
    End Property

    Public Property GetAwayTeam() As Team
        Get
            Return _AwayTeam
        End Get
        Set(ByVal value As Team)

        End Set
    End Property

    Public Property GetGameNumber() As Integer
        Get
            Return _Game
        End Get
        Set(ByVal value As Integer)

        End Set
    End Property
End Class

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
joriszwaenepoelCommented:
You can use "Select New Team(temaName)" in your LINQ query:

HomeList = (From c In League Where c.GetGameNumber = (dayNumber - 1) Select New Team(c.GetAwayTeam)).ToList
0
 
DhaestAuthor Commented:
@Webtubbs: Thanks for the insight (overlooked the fact that I declared the team as string in the class Game)
@JorisZwaenepoel: thx
0
 
DhaestAuthor Commented:
@JorisZwaenepoel: I was too quick to cheer ... I tried your solution, but I get a list of teams back where each team is always the same one !
0
 
joriszwaenepoelCommented:
That should not be the case.

If you get the results without the "As New", you get a list of strings.  If that list is correct then using the "As New" technique should give correct results as well.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now