我想選擇所有圖片并計算 IsAnnotated 欄位,但是如何選擇 * 欄位并應用此 IsAnnotated 欄位?因為在這個例子中現在只選擇了 IsAnnotated。我們是否有可能阻止手動撰寫諸如 Id = picture.Id、title = picture.Title 之類的內容?
var picture = await _dbContext.Pictures
.Select(picture => new Picture
{
IsAnnotated = _dbContext.Annotations.Any(x => x.PictureId == picture.Id),
})
.Where(s => ids.Contains(s.Id))
.ToListAsync();
uj5u.com熱心網友回復:
首先,通過 ids 串列選擇您想要的圖片:
var pictures = _dbContext.Pictures
.Where(s => ids.Contains(s.Id));
其次,用 foreach 計算 IsAnnotated 欄位——這不是“選擇”的情況,這是更新的情況。(轉換為“串列”以使用 ForEach linq 函式,您可以改用 foreach 陳述句):
pictures.ToList().ForEach(a => a.IsAnnotated = _dbContext.Annotations.Any(x => x.PictureId == a.Id));
uj5u.com熱心網友回復:
你可以做這樣的事情。
Var pictures = _dbcontext.Pictures.Where(x=> IDs.Contains(x.id));
var annotations = await pictures.Join(_dbContext.annotation, pic => pic.id, ann => ann.pictureId, (pic, ann) => new
{
Annotation = ann.IsAnnotated
}.ToListAsync();
或者,如果需要,您可以傳遞一個模型來代替匿名型別。
對不起,如果它不可讀,但我是用手機寫的!??
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/445660.html
