question

Matt-5257 avatar image
0 Votes"
Matt-5257 asked YijingSun-MSFT answered

Mapping fields from multiple tables to model in MVC entity framework

Hi,

I'm trying to map fields from two tables to a model but I keep getting the following error;

The entity or complex type cannot be constructed in a LINQ to Entities query.

Here is my code. Can anyone tell me what I'm doing wrong please?

[HttpGet, Authorize]
public ActionResult GetAssetTypeChecks(bool? isActive = null)
{
try
{
// List<AssetTypeCheck> assetTypeCheck = CompanyDatabase.AssetTypeChecks.AsNoTracking().OrderBy(x => x.Id).ToList();

             List<AssetTypeCheck> assetTypeCheck = (from app in CompanyDatabase.AssetTypeChecks
                      join assettype in CompanyDatabase.AssetTypes on app.AssetTypeId equals assettype.Id
                      select new AssetTypeCheck
                      {
                          Id = app.Id,
                          Name = app.Name,
                         CheckType = app.CheckType,
                         Description = app.Description,
                          OrderNum = app.OrderNum
                     }).ToList();


             return Json(new { success = true, result = assetTypeCheck }, JsonRequestBehavior.AllowGet);
         }
         catch (Exception exception)
         {
             return Json(new { success = false, message = exception.Message }, JsonRequestBehavior.AllowGet);
         }
     }
dotnet-aspnet-mvc
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @Matt-5257 ,
As far as I think,you cannot (and should not be able to) project onto a mapped entity. The project could onto an anonymous type: IEnumerable<> or onto a DTO.
Best regards,
Yijing Sun


If the answer is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.