将选择查询数据绑定到viewmodel列表

时间:2014-12-19 03:10:56

标签: c# asp.net-mvc entity-framework

我试图将选择查询数据绑定到我的viewmodel。任何人都可以建议如何更换问号?我需要为每个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();

1 个答案:

答案 0 :(得分:1)

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

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();

HTH