C# LINQ的左外部联接运行时错误

C# LINQ的左外部联接运行时错误,c#,mysql,linq,C#,Mysql,Linq,我试图在2个表上使用Linq编写一个左外部联接查询,但在运行时,当右表中有空值时,会出现空引用异常 对于tbl_Match table上的所有MatchID值,tbl_UserBets table没有所有值。因此,当出现NULL值时,我将得到运行时异常 PFB我的LINQ查询 string userID = "dfa3c0e7-2aa3-42ee-a7d3-803db902dc56"; var res2 = dbEntity.tbl_Match.Select(m => new

我试图在2个表上使用Linq编写一个左外部联接查询,但在运行时,当右表中有空值时,会出现空引用异常

对于tbl_Match table上的所有MatchID值,tbl_UserBets table没有所有值。因此,当出现NULL值时,我将得到运行时异常

PFB我的LINQ查询

string userID = "dfa3c0e7-2aa3-42ee-a7d3-803db902dc56";
var res2 = dbEntity.tbl_Match.Select(m => new
            {
                MatchID = m.MatchID,
                Team1 = m.Team1,
                Team2 = m.Team2,
                UserForTeam1 = dbEntity.tbl_UserBets.Where(b => b.UserForTeam1 == userID).FirstOrDefault(b => b.MatchID == m.MatchID),
                UserForTeam2 = dbEntity.tbl_UserBets.Where(b => b.UserForTeam2 == userID).FirstOrDefault(b => b.MatchID == m.MatchID)
            });

            foreach (var item in res2)
            {
                Console.WriteLine(item.MatchID + " " + item.Team1 + " vs " + item.Team2 + " " + item.UserForTeam1 == null ? " NA " : item.UserForTeam1.UserForTeam1);
            }
tbl_匹配表的SQL表设计:

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
);
tbl_UserBets表的SQL表设计:

Create Table tbl_UserBets(
UserBetSlNo int primary key identity,
TournamentID int Foreign key references tbl_Tournament(TournamentID),
MatchID int Foreign key references tbl_Match(MatchID),
UserForTeam1 nvarchar(128) Foreign key references AspNetUsers(Id),
UserForTeam2 nvarchar(128) Foreign key references AspNetUsers(Id),
UserForNoBets nvarchar(128) Foreign key references AspNetUsers(Id)
);
使用SQL中的以下查询,我能够正确地获得结果,需要对LINQ执行同样的操作

select DISTINCT(tbl_Match.MatchID),tbl_Match.Team1,tbl_Match.Team2,tbl_Match.StartTime,tbl_Match.MatchBetAmount,tbl_UserBets.UserForTeam1,tbl_UserBets.UserForTeam2,tbl_UserBets.UserForNoBets from tbl_Match left outer join tbl_UserBets on tbl_Match.MatchID = tbl_UserBets.MatchID and (tbl_UserBets.UserForTeam1 = 'dfa3c0e7-2aa3-42ee-a7d3-803db902dc56' or tbl_UserBets.UserForTeam2 = 'dfa3c0e7-2aa3-42ee-a7d3-803db902dc56')

请让我知道,我应该做什么改变来解决这个问题。谢谢。

尝试将where条件与第一个或默认条件合并

我猜问题在于where返回null时,first或default将导致异常


请参阅msdn:感谢您回复我的问题。问题在于我是如何在foreach循环中将null值与console.writeline中的其他字符串值连接起来的,bcoz是其中的null指针异常。有一次,我移动了逻辑来检查循环之前的值是否为null。它解决了这个问题。没问题,我会留下我的answear也许可以帮助其他人解决我描述的问题
var res2 = dbEntity.tbl_Match.Select(m => new
            {
                MatchID = m.MatchID,
                Team1 = m.Team1,
                Team2 = m.Team2,
                UserForTeam1 = dbEntity.tbl_UserBets.FirstOrDefault(b => b.UserForTeam1 == userID && b.MatchID == m.MatchID),
                UserForTeam2 = dbEntity.tbl_UserBets.FirstOrDefault(b => b.UserForTeam2 == userID && b.MatchID == m.MatchID)
            });