I'm just getting to grips with LINQ and I have the following scenario:
I have a database which represents sports matches between teams on various dates, organised as follows:
Table MATCH
___________
MatchID
MatchLocation
MatchDate
Table MATCHTEAM
________________
MatchID
TeamID
HomeOrAway
Points
Table TEAM
__________
TeamId
TeamName
Now I want to be able to query the database to look at all matches a particular team played in, and the associated team data for each match. So far I've been able to perform a query that does a GROUP JOIN as follows:
Using MyMatchEntites As New MatchEntities
Return (From M In MyMatchEntites.Match, _
MT In MyMatchEntites.MatchTeam _
Where M.MatchId = MT.Match.MatchId And _
MT.Team.TeamId = inputTeamId _
Select M _
Group Join MT2 In MyMatchEntites.MatchTeam _
开发者_运维百科 On MT2.Match.MatchId Equals M.MatchId _
Into _
MatchTeams = Group).ToList
End Using
This gives me a list of matches played in by the input team and each record has an associated list of the teams that played in the match. However, I also want to include the team name in this sublist for display purposes. How would I include the TeamName field in the MatchTeams group created in the query?
Just a guess...
Is it possible to do something like that:
Select M, T _
Group Join MT2 In MyMatchEntites.MatchTeam _
On MT2.Match.MatchId Equals M.MatchId _
Group Join T In MyMatchEntites.Team
On T.TeamId = MT2.TeamId
I'm pretty sure that you can do multiple GroupJoins with LINQ, but I don't know about retrieving multiple entities in the select clause. By the way, this would return the whole Team entity, not only the field TeamName.
Hope this helps.
精彩评论