将查询转换为列表(不工作)asp.net mvc

时间:2014-09-10 15:49:06

标签: asp.net asp.net-mvc database linq

我正在尝试创建一个查询,其结果将存储在ViewModel中。当我尝试将查询变量分配给ViewModel的方法时,我收到以下错误:“无法将类型'System.Linq.Iqueryable'隐式转换为'System.Collections.Generic.List'。存在显式转换(是吗?错过演员?)“。 我尝试将我的ViewModel方法更改为Iqueryable类型而不是List,但是我无法在视图中使用foreach循环来遍历我的模型,所以我将其更改回List。我尝试在查询后执行ToList(),但这也不起作用。任何建议/提示/提示非常感谢。下面是我的控制器代码和我的ViewModel代码。

视图模型:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KU_PLAN_DEV.Models;

namespace KU_PLAN_DEV.ViewModels
{
public class TrackViewModel
{
    public List<string> TRACK_INFO { get; set; }
    public List<string> GEN_ED_HEAD { get; set; }
}
}

控制器方法:

 public ActionResult DisplayTrackSheet(string trackButton)
    {
        var db = new KuPlanEntities();

        var trackProgNum = (from info in db.TRACK_INFO
                            where info.degreeName == trackButton
                            select info.progNum).ToString();

        var trackVerNum = (from info in db.TRACK_INFO
                           where info.degreeName == trackButton
                           select info.versionNum).ToString();

        /*var queryTrack = (from tracks in db.GEN_ED_HEAD
                          where tracks.)*/

        var trackData = (from trackInfo in db.TRACK_INFO
                         where trackInfo.progNum == trackProgNum
                         && trackInfo.versionNum == trackVerNum
                         select trackInfo);

        var trackDisplayMod = new TrackViewModel
        {
            TRACK_INFO = trackData
        };

        return View(trackDisplayMod);
    }

1 个答案:

答案 0 :(得分:0)

    var trackDisplayMod = new TrackViewModel
    {
        TRACK_INFO = trackData.ToList()
    };

进行更改后会出现什么错误?