Solved

Linq result to object

Posted on 2010-09-01
7
324 Views
Last Modified: 2013-11-11
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
Comment
Question by:Dhaest
  • 3
  • 2
  • 2
7 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 33584192
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
 
LVL 53

Author Comment

by:Dhaest
ID: 33584236
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
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 250 total points
ID: 33584260
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 10

Assisted Solution

by:joriszwaenepoel
joriszwaenepoel earned 250 total points
ID: 33584294
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
 
LVL 53

Author Closing Comment

by:Dhaest
ID: 33584361
@Webtubbs: Thanks for the insight (overlooked the fact that I declared the team as string in the class Game)
@JorisZwaenepoel: thx
0
 
LVL 53

Author Comment

by:Dhaest
ID: 33584403
@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
 
LVL 10

Expert Comment

by:joriszwaenepoel
ID: 33584437
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now