美好的一天,我有這 2 個模型
public partial class PostPhoto
{
public Guid Id { get; set; }
public string Path { get; set; }
public string Filename { get; set; }
public int? User { get; set; }
public Guid? Post { get; set; }
}
和
public partial class Post
{
public Guid Id { get; set; }
public string Text { get; set; }
public int? User { get; set; }
public DateTime? DateCreated { get; set; }
}
我想把這 2 加入這個 ViewModel
public class PostViewModel
{
public Guid Id { get; set; }
[Required]
public String PostBody { get; set; }
public string Image { get; set; }
public int UserId { get; set; }
public string UserFullname { get; set; }
public DateTime DateCreated { get; set; }
public List<Images> ImageList { get; set; }
}
public class Images
{
public string ImagePath { get; set; }
}
我有這個連接查詢
(from post in _context.Post
join user in _context.AspNetUsers
on post.User equals user.Id
join photos in _context.PostPhoto
on post.Id equals photos.Post into phtos
orderby post.DateCreated descending
select new PostViewModel {
Id = post.Id,
UserId = user.Id,
UserFullname = string.Format("{0} {1}", user.Firstname, user.LastName),
PostBody = post.Text,
DateCreated = (DateTime)post.DateCreated,
}).ToList();
問題是我不知道如何List<Images>在 join 陳述句的結果中填充 。有沒有人知道如何實作這個價值?
uj5u.com熱心網友回復:
您不需要加入照片。只需在投影中填寫串列
var query =
from post in _context.Post
join user in _context.AspNetUsers on post.User equals user.Id
orderby post.DateCreated descending
select new PostViewModel
{
Id = post.Id,
UserId = user.Id,
UserFullname = string.Format("{0} {1}", user.Firstname, user.LastName),
PostBody = post.Text,
DateCreated = (DateTime)post.DateCreated,
ImageList = _context.PostPhoto
.Where(p => post.Id == p.Post)
.Select(p => new Images { ImagePath = p.Path })
.ToList()
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/455320.html
標籤:实体框架 林克 加入 实体框架核心 asp.net-core-mvc
