0

I have a table with multiple relationships and I am trying to display multiple columns from 2 different tables. I have a linq statement however when I run this code, I get the following error:

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

My code:

public ActionResult ActionItems(string Status)
{
    tblActionItem actionitems = new tblActionItem();
    if (Status == null)
    {
        var incc = (from sa in db.tblActionItems
                    join trid in db.tblTripReports on sa.TripReportID equals trid.tripreportID
                    join cust in db.tblCustomers on trid.Customer_ID equals cust.CustomerID
                    join emp in db.tblEmployees on cust.EmployeeID equals emp.EmployeeID
                    select
                     new tblActionItem
                     {
                         Status = sa.Status,
                         Action_Item = sa.Action_Item,
                         Owners = sa.Owners,
                         Due_Date = sa.Due_Date,
                         Updated = sa.Updated,
                         CreateDate = sa.CreateDate,
                         Sales = emp.Sales
                     }).ToList();

        return View(incc);

        //return View(db.tblActionItems.ToList());
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user9448003
  • 87
  • 1
  • 10

1 Answers1

0

You should possibly use an intermediate state.

var intermediate = (from sa in db.tblActionItems
                    join trid in db.tblTripReports on sa.TripReportID equals trid.tripreportID
                    join cust in db.tblCustomers on trid.Customer_ID equals cust.CustomerID
                    join emp in db.tblEmployees on cust.EmployeeID equals emp.EmployeeID
                    select
                     new 
                     {
                         Status = sa.Status,
                         Action_Item = sa.Action_Item,
                         Owners = sa.Owners,
                         Due_Date = sa.Due_Date,
                         Updated = sa.Updated,
                         CreateDate = sa.CreateDate,
                         Sales = emp.Sales
                     }).ToList();
var result = intermediate.Select(x=> new tblActionItem {
                    Status = x.Status,
                    Action_Item = x.ActionItem,
                   Owners = x.Owners,
                   Due_Date = x.DueDate;
                   Updated =x.Update,
                   CreateDate = x.CreateDate,
                   Sales = x.Sales
    });
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51