无法在LINQ to Entities查询中构造。错误

时间:2015-08-05 03:29:14

标签: c# linq entity-framework

public ActionResult CreateApp(Guid id)
    {

        SMICParkingLotApplicationEntities1 dbb = new SMICParkingLotApplicationEntities1();
        ApplicationDATA applicationData = (from a in dbb.ApplicationDATAs
            where a.ApplicationID == id
            select new ApplicationDATA
            {
                ApplicationID = a.ApplicationID,
                BrandModel = a.BrandModel,
                CrNo = a.CrNo,
                OrNo = a.OrNo,
                DatePosted = a.DatePosted,
                PoR = a.PoR,
                PlateNo = a.PlateNo,
                VehicleType = a.VehicleType

            }).FirstOrDefault();

        ApplicationSlotViewModel applicationSlotViewModel = new ApplicationSlotViewModel
        {
            ApplicationDatas = applicationData,
            Application = new Application()
        };
        return View(applicationSlotViewModel);

Dunno做什么总是显示此错误无法在LINQ to Entities查询中构建。错误帮助Plss ..

2 个答案:

答案 0 :(得分:0)

在new关键字后删除className。请尝试以下代码。

    var applicationData = (from a in dbb.ApplicationDATAs
        where a.ApplicationID == id
        select new 
        {
            ApplicationID = a.ApplicationID,
            BrandModel = a.BrandModel,
            CrNo = a.CrNo,
            OrNo = a.OrNo,
            DatePosted = a.DatePosted,
            PoR = a.PoR,
            PlateNo = a.PlateNo,
            VehicleType = a.VehicleType

        }).FirstOrDefault();

答案 1 :(得分:0)

如果您的ViewModel中的ApplicationDatas类型为ApplicationDATA,您可以直接使用查询结果进行设置:

var applicationData =dbb.ApplicationDATAs.FirstOrDefault(a=>a.ApplicationID == id);
ApplicationSlotViewModel applicationSlotViewModel = new ApplicationSlotViewModel
{
        ApplicationDatas = applicationData,
        Application = new Application()
};

您无法使用现有实体类型投影查询结果。检查我在评论中引用的post