我无法弄清楚如何在扩展方法中使用linq进行简单的左连接。
我需要使用扩展方法而不是理解查询,因为我正在分页结果。
var carParkPagedList =
db.CarPark
.Join(db.UserProfiles, cp => cp.UserProfileId, up => up.UserId, (cp, up))
.OrderBy(f => f.Name)
.Where(f => f.FloorId == floorId)
.Select(f => new CarParkListDisplayModel
{
CarParkId = f.CarParkId,
Name = f.Name
}).ToPagedList(page, 10);
我无法在任何地方找到帮助,我已经尝试了101个linq示例,这是查找理解查询但不会为扩展方法提供任何支持。
答案 0 :(得分:1)
好的,谢谢西蒙,这种方法有效,我将从现在开始使用它,更合乎逻辑。
var carParkPagedList =
(from cp in db.CarPark
where cp.FloorId == floorId
join occupant in db.UserProfiles on cp.UserProfileId equals occupant.UserId into gj
from occupant in gj.DefaultIfEmpty()
orderby cp.Name
select new CarParkListDisplayModel
{
CarParkId = cp.CarParkId,
Name = cp.Name,
UsersName = (occupant == null ? "Un-ocupied" : occupant.FirstName + " " + occupant.LastName)
}).ToPagedList(page, 10);