将选择查询数据绑定到视图模型列表

本文关键字:视图 模型 列表 数据绑定 选择 查询 | 更新日期: 2023-09-27 18:34:43

我正在尝试将选择查询数据绑定到我的视图模型。任何人都可以建议如何替换问号吗?我需要为每个 FItemVM 添加一个要List<FItemVM>的项目。

视图模型

public class FListVM
{
    public string Title { get; set; }
    public DateTime Posted { get; set; }
    public List<FItemVM> FItemVMs { get; set; }
}
    public class FItemVM
    {
        public string Name { get; set; }
        public int FItemSum { get; set; }
    }

控制器

var fListItem = db.FListItems.Include(f => f.FList)
                             .Include(f => f.FItem)
                             .Select(f => new FListVM
                                {
                                    Title = f.FList.Title,
                                    Posted = f.FList.Posted,
                                    FItemVMs = new List<FItemVM>()
                                    {
                                        ???
                                    }
                                }).OrderByDescending(f => f.Posted).ToList();

将选择查询数据绑定到视图模型列表

由于它是一个集合,您可以按如下方式添加它

var fListItem = db.FListItems.Include(f => f.FList)
                             .Include(f => f.FItem)
                             .Select(f => new FListVM
                                {
                                    Title = f.FList.Title,
                                    Posted = f.FList.Posted,
                                    FItemVMs = f.FList.Where(fl=>fl.Title==f.FList.Title).Select(fl=>f.FList.Items).ToList()
                                }).OrderByDescending(f => f.Posted).ToList();