I have the following code that works fine:
var model = db.TankCentres
.Include(images => images.ProfileImages)
.Where(t => t.Live == true)
.ToList();
ProfileImages is a collection but I only want to take the first item, so I want to do something like:
var model = db.TankCentres
.Include(images => images.ProfileImages.FirstOrDefault())
.Where(t => t.Live == true)
.ToList();
but this does not work.
How can I achieve what I'm looking to do?
How about this?
var model = db.TankCentres.Where(t => t.Live == true)
.Select(i => new { ProfileImage = i.ProfileImages.FirstOrDefault(), TankCentre = i} )
.ToList();
I used this is the end:
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()
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.