简体   繁体   中英

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.

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.

What I ended up doing was:

        foreach (BillPaymentSummary payment in billPayments)
        {
            var data = db.BillHistories.Where(b => b.CustomerId == customerNumber && b.DueDate == payment.DueDate && b.Type == "B").FirstOrDefault();

            if (data != null) // There is a bill history
            {
                returnSummaries.Add(new BillSummary
                {
                    Id = data.Id,
                    CustomerId = data.CustomerId,
                    DueDate = data.DueDate,
                    PreviousBalance = data.PreviousBalance,
                    TotalBill = data.TotalBill,
                    Type = (data.Type.Trim() == "B" ? "BILL" : (data.Type == "A" ? "ADJ" : "")),
                    IsFinalBill = data.IsFinalBill,
                    PayDate = payment.PaidDate,
                    AmountPaid = payment.AmountPaid
                });
            }
            else // No bill history record, look for an adjustment
            {
                data = db.BillHistories.FirstOrDefault(b => b.CustomerId == customerNumber && b.DueDate == payment.DueDate && b.Type == "A");

                if (data != null)
                {
                    returnSummaries.Add(new BillSummary
                    {
                        Id = data.Id,
                        CustomerId = data.CustomerId,
                        DueDate = data.DueDate,
                        PreviousBalance = data.PreviousBalance,
                        TotalBill = data.TotalBill,
                        Type = (data.Type.Trim() == "B" ? "BILL" : (data.Type == "A" ? "ADJ" : "")),
                        IsFinalBill = data.IsFinalBill,
                        PayDate = payment.PaidDate,
                        AmountPaid = payment.AmountPaid
                    });
                }
            }
            db.SaveChanges();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM