无法在LINQ to Entities查询中构造实体或复杂类型

时间:2011-12-12 15:33:04

标签: c# linq entity-framework-4.1

在我们的在线结算应用程序中,我们会提供客户收到的账单及其付款的结算摘要。

为了实现这一点,我必须先付款然后将它们与账单相匹配。所以我做了类似的事情:

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

我一直在玩这个,但无论我做什么,我都会收到以下错误消息:

  

无法在LINQ to Entities查询中构造实体或复杂类型“UtilityBill.Domain.Concrete.BillSummary”。

是因为我在查询中运行查询吗?我怎样才能解决这个错误?

我尝试过在Google上搜索答案并看到很多答案,但似乎没有一个能解释我的问题。

2 个答案:

答案 0 :(得分:6)

您无法投影到映射的实体。在进行映射之前,您必须先调用ToList()

或者更好的是,更改为以下内容(调用FirstOrDefault将执行查询并允许您填充对象):

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

要将自己与实体框架分离,您可能还需要考虑使用不同的模型类而不是实体框架模型。

答案 1 :(得分:0)

我最终做的是:

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