在linq上左外连接三个表

时间:2015-07-08 06:10:09

标签: c# linq

 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表中获取所有数据

1 个答案:

答案 0 :(得分:0)

尝试此查询

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