Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

On our online billing application, we give a billing summary of what bills the customer received and the payments they made.

In order for this to work, I have to first pull the payments then match them to the bills. So I have do something like:

foreach (BillPaymentSummary payment in billPayments)
{
    DateTime dt = payment.DueDate;

    // Debug errors on this next line
    var summary = (from a in db.BillHistories
                   where a.CustomerId == customerNumber && a.DueDate == dt && a.Type == "BILL"
                   select new BillSummary
                   {
                       Id = a.Id,
                       CustomerId = a.CustomerId,
                       DueDate = a.DueDate,
                       PreviousBalance = a.PreviousBalance.Value,
                       TotalBill = a.TotalBill.Value,
                       Type = a.Type,
                       IsFinalBill = a.IsFinalBill
                   }).SingleOrDefault();

    if (summary != null)
    {
        summary.PayDate = payment.PaidDate;
        summary.AmountPaid = payment.AmountPaid;
        returnSummaries.Add(summary);
    }
    else
    {
        summary = (from a in db.BillHistories
                   where a.CustomerId == customerNumber && a.DueDate == payment.DueDate && a.Type == "ADJ "
                   select new BillSummary
                   {
                       Id = a.Id,
                       CustomerId = a.CustomerId,
                       DueDate = a.DueDate,
                       PreviousBalance = a.PreviousBalance.Value,
                       TotalBill = a.TotalBill.Value,
                       Type = a.Type,
                       IsFinalBill = a.IsFinalBill
                   }).SingleOrDefault();

        if (summary != null)
        {
            summary.PayDate = payment.PaidDate;
            summary.AmountPaid = payment.AmountPaid;
            returnSummaries.Add(summary);
        }
    }
}

I have been playing with this, but no matter what I do, I get the following error message:

The entity or complex type 'UtilityBill.Domain.Concrete.BillSummary' cannot be constructed in a LINQ to Entities query.

Is it because I am running queries within queries? How can I get around this error?

I have tried searching Google for an answer and see many answers, but none of them seem to explain my problem.

like image 378
Mike Wills Avatar asked Dec 12 '11 15:12

Mike Wills


1 Answers

You cannot project onto a mapped entity. You would have to call ToList() before doing your mapping.

Or better yet, change to the following (calling FirstOrDefault will execute the query and allow you to populate your object):

var summary = db.BillHistories.FirstOrDefault(a => a.CustomerId == customerNumber && a.DueDate == dt && a.Type == "BILL").Select(x => new BillSummary
                               {
                                   Id = a.Id,
                                   CustomerId = a.CustomerId,
                                   DueDate = a.DueDate,
                                   PreviousBalance = a.PreviousBalance.Value,
                                   TotalBill = a.TotalBill.Value,
                                   Type = a.Type,
                                   IsFinalBill = a.IsFinalBill
                               });

To decouple yourself from the Entity Framework you may want to also consider using a different model class to return instead of the Entity Framework model.

like image 182
Craig Avatar answered Nov 19 '22 04:11

Craig