我试图用LINQ
编写类似的查询select DISTINCT(m.MatchID),
(select TeamName
from tbl_TournamentTeams
where TeamID = m.Team1),
(select TeamName
from tbl_TournamentTeams
where TeamID = m.Team2)
from tbl_TournamentTeams t, tbl_Match m
where t.TournamentID = m.TournamentID
SQL tbl_TournamentTeams表设计,
Create Table tbl_TournamentTeams(
TeamID int primary key Identity,
TournamentID int Foreign key references tbl_Tournament(TournamentID),
TeamName varchar(30) NOT NULL
);
SQL tbl_Match表设计,
Create Table tbl_Match(
MatchID int primary key Identity,
TournamentID int Foreign key references tbl_Tournament(TournamentID),
Team1 int Foreign key references tbl_TournamentTeams(TeamID),
Team2 int Foreign key references tbl_TournamentTeams(TeamID),
StartTime DateTime not null,
MatchBetAmount int not null
);
请让我知道我能用LINQ写一个类似的查询。感谢
我想从linq查询获得每个匹配的TeamName,目前我只能获取teamId进行匹配。请通过以下查询告诉我如何获取TeamName详细信息
var res = dbEntity.tbl_Match.Join(dbEntity.tbl_TournamentTeams,
m => m.TournamentID,
t => t.TournamentID,
(m, t) => new
{
MatchID = m.MatchID,
MatchTeam1 = m.Team1,
MatchTeam2 = m.Team2,
TournamentID =t.TournamentID
// How to print the team name details as well here, where currently i'm just able to fetch the teamID
});
答案 0 :(得分:1)
我认为team1和team2不包含空值
dbEntity.tbl_Match.Select(m => new{
m.Id,
Team1Name=dbEntity.TournamentTeams.SingleOrDefault(t => t.id=m.Team1).Name,
Team2Name =dbEntity.TournamentTeams.SingleOrDefault(t => t.id=m.Team2).Name
})