简体   繁体   English

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

[英]Left outer join runtime error with LINQ

I'm trying to write a left outer join query with Linq on 2 tables, but getting NULL reference exception during runtime, when there are null values in the right table. 我正在尝试在2个表上用Linq编写一个左外部联接查询,但是在右表中有空值的情况下,运行时会得到NULL引用异常。

For all MatchID values as on tbl_Match table tbl_UserBets table does not have values for all. 对于tbl_Match表中的所有MatchID值,tbl_UserBets表没有所有值。 Hence when NULL value comes up, I'm getting run time exception 因此,当出现NULL值时,我得到了运行时异常

PFB my LINQ query, 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);
            }

SQL Table design for tbl_Match table: tbl_Match表的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
);

SQL Table design for tbl_UserBets table: 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)
);

With the below query in SQL i'm able to get the results properly, Need to do the same with LINQ. 使用下面的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')

Please let me know, what changes i should be doing to fix the issue. 请让我知道,我应该怎么做才能解决此问题。 Thanks. 谢谢。

try merging the where condition with the first or default 尝试将where条件与第一个或默认值合并

i'm guessing the problem is when the where returns null the first or default will cause the exception as stated by msdn doc 我猜问题是当where返回null时,第一个或默认值将导致msdn doc所述的异常

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)
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM