2

I have a following object structure

a Group has Many Teams and a Team has many Players

m_Groups is an observable collection of groups

I'm trying to get the Team name given a group name and a players name I can do this like in for each, but having trouble converting it to LINQ:

Dim retVal As String = String.empty  

For Each g In m_Groups
  For Each t In g.Teams
    For Each p In t.Players
      If p.PlayerName.Equals(name) Then
        retVal = t.TeamName
      End If
    Next
  Next
Next

3 Answers 3

2
Dim teamName = (From group in Groups _
                From team In group.Teams _
                From player In team.Players _
                Where player.PlayerName.Equals(name) _
                Select team.TeamName).FirstOrDefault()

(Disclaimer: I'm mostly guessing the VB syntax above.)

Also, my actual way of dealing with situations like youre would be to give players a Team property and connect them up once after loading instead of doing a lookup every time. (Assuming the player assignments don't change, a bidirectional connection would require some boilerplate to maintain.)

Sign up to request clarification or add additional context in comments.

Comments

1

There are obviously several ways of doing it, here is what came to my mind

  var team = m_Groups
       .SelectMany(g=>g.Teams)
       .FirstOrDefault(t=>t.Players.Any(p=>p.PlayerName == name));

I hope you'll be able to convert it to VB yourself

Comments

1

With your setup, I am assuming you would perform a LINQ query every time you need to search for a particular team. A better approach would be to have a linear structure, such as a dictionary, which has name as a key.

Keeping close to what you got there, here is some code to play with:

Option Strict On

Public Class Form1
  Class MyGroup
    Public Teams As New List(Of MyTeam)
  End Class
  Class MyTeam
    Public Players As New List(Of MyPlayer)
    Public TeamName As String
  End Class
  Class MyPlayer
    Public PlayerName As String
  End Class

  Dim m_Groups As New List(Of MyGroup)

  Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    Dim p As New MyPlayer With {.PlayerName = "Player"}
    Dim t As New MyTeam With {.TeamName = "Team", .Players = New List(Of MyPlayer)({p})}
    m_Groups.Add(New MyGroup With {.Teams = New List(Of MyTeam)({t})})
    Dim name As String = "Player" 'player name to be searched

    Dim teamName = (From group In m_Groups _
                    From team In group.Teams _
                    From player In team.Players _
                    Where player.PlayerName.Equals(name) _
                    Select team.TeamName).FirstOrDefault()
  End Sub
End Class

It was heavily influenced by @millimoose's answer, with two slight changes:

  1. Dim team -> Dim teamName, you cannot have two team variables in the same scope.
  2. OP wants the team name, so Select player became Select team.TeamName.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.