我有以下代码可以正常工作:
var model = db.TankCentres
.Include(images => images.ProfileImages)
.Where(t => t.Live == true)
.ToList();
ProfileImages是一个集合,但我只想拍摄第一个项目,所以我想做类似的事情:
var model = db.TankCentres
.Include(images => images.ProfileImages.FirstOrDefault())
.Where(t => t.Live == true)
.ToList();
但这不起作用。
我如何实现我的目标?
答案 0 :(得分:0)
这个怎么样?
var model = db.TankCentres.Where(t => t.Live == true)
.Select(i => new { ProfileImage = i.ProfileImages.FirstOrDefault(), TankCentre = i} )
.ToList();
答案 1 :(得分:0)
我用过这个结尾:
var model = from t in db.TankCentres
where t.Live == true
select new ViewAllTanksViewModel
{
Id = t.Id,
Name = t.Name,
Address = t.Address,
Live = t.Live,
ExperienceId = t.ExperienceId,
MainImageVM = t.ProfileImages.FirstOrDefault()
};