不能在LINQ到实体查询中构造.错误

本文关键字:错误 查询 实体 LINQ 不能 | 更新日期: 2023-09-27 18:06:28

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

不知道该怎么做它总是显示这个错误不能在LINQ到实体查询中构造。错误帮助

不能在LINQ到实体查询中构造.错误

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

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

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

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