在三个表的linq中左外连接

本文关键字:linq 连接 三个 | 更新日期: 2023-09-27 18:01:27

 var response = ( from e in db.tblEvents                  
                  join f in db.tblEventTypes on e.FightTypeId equals f.eventTypeId
                  into egroup
                  from e in egroup.DefaultIfEmpty()
                  join w in db.tblUserWebApp on e.ModifiedUserId equals w.Id
                  orderby e.LastUserModified descending
                  select new {
                            FightTypeName  = f.eventTypeName,
                            EventID =  e.EventID,
                            FightTypeId=e.FightTypeId,
                            Title = e.Title,
                            Date = e.Date,
                            Location = e.Location, 
                            UserSelectFavoriteFlag =e.UserSelectFavoriteFlag ,
                            Price=e.Price,
                            UserPredictionFlag=e.UserPredictionFlag,
                            PredictionStartDate=   e.PredictionStartDate ,
                            PredictionEndDate   = e.PredictionEndDate,
                            ModifiedUserId = w.Id,
                            ModifiedUser = w.LoginName,
                            LastUserModified = e.LastUserModified,
        });
        return Ok(response);

我如何使用左外连接,因为我有3个表加入,我想从tblEvents表

在三个表的linq中左外连接

获得所有数据

试试这个查询

var response = ( from e in db.tblEventTypes              
                  from f in db.tblEvents.where(x=>x.FightTypeId ==e.eventTypeId).DefaultIfEmpty()
                  from w in db.tblUserWebApp.where(x=>x.Id==f.ModifiedUserId)
                  orderby f.LastUserModified descending
                  select new {
                            FightTypeName  = e.eventTypeName,
                            EventID =  f.EventID,
                            FightTypeId=f.FightTypeId,
                            Title = f.Title,
                            Date = f.Date,
                            Location = f.Location, 
                            UserSelectFavoriteFlag =f.UserSelectFavoriteFlag ,
                            Price=f.Price,
                            UserPredictionFlag=f.UserPredictionFlag,
                            PredictionStartDate=   f.PredictionStartDate ,
                            PredictionEndDate   = f.PredictionEndDate,
                            ModifiedUserId = w.Id,
                            ModifiedUser = w.LoginName,
                            LastUserModified = f.LastUserModified,
        });