3

我有一个问题需要我数周才能解决,但我一直没能解决。

我有一堂课,我有两种方法。以下应该从数据库中获取最新日期。该日期代表客户对“某事”所做的最新付款:

public DateTime getLatestPaymentDate(int? idCustomer)
{
    DateTime lastDate;

    lastDate = (from fp in ge.Payments
                from cst in ge.Customers
                from brs in ge.Records.AsEnumerable()
                where (cst.idCustomer == brs.idCustomer && brs.idHardBox == fp.idHardbox
                       && cst.idCustomer == idCustomer)
                select fp.datePayment).AsEnumerable().Max();

    return lastDate;
}//getLatestPaymentDate

在这里我有另一种方法,它应该调用前一个方法来完成一个 Linq 查询并将其传递给一个 Crystal Report:

//Linq query to retrieve all those customers'data who have not paid their safebox(es) annuity in the last year.
public List<ReportObject> GetPendingPayers()
{
    List<ReportObject> defaulterCustomers;

      defaulterCustomers = (from c in ge.Customer
                            from br in ge.Records
                            from p in ge.Payments

                            where (c.idCustomer == br.idCustomer
                                   && br.idHardBox == p.idHardBox)

                            select new ReportObject
                            {
                                CustomerId = c.idCustomer,
                                CustomerName = c.nameCustomer,
                                HardBoxDateRecord = br.idHardRecord,
                                PaymentDate = getLatestPaymentDate(c.idCustomer),
                            }).Distinct().ToList();
}//GetPendingPayers

这里没有抛出编译错误,但是当我运行应用程序并且第二种方法尝试调用该字段中的第一个方法PaymentDate时,会出现标题中提到的错误:

Linq to Entities does not recognize the method System.DateTime.. and cannot translate this into a store expression

请任何有有用输入的人让我摆脱这个混乱的错误?任何帮助将不胜感激 !

非常感谢 !

4

1 回答 1

1

看看这些其他问题:

LINQ to Entities 无法识别该方法

LINQ to Entities 无法识别方法“System.DateTime Parse(System.String)”方法

基本上,您不能在 C# 端使用值并将其转换为 SQL。第一个问题提供了更彻底的解释;第二个为您的问题提供了一个简单的解决方案。

编辑 :

简而言之:EF 要求 SQL 服务器执行该getLatestPaymentDate方法,但它对此一无所知。您需要在程序端执行它。

只需先执行查询,将结果放入列表中,然后Select在内存列表中执行:

 List<ReportObject> defaulterCustomers;

  var queryResult = (from c in ge.Customer
                        from br in ge.Records
                        from p in ge.Payments

                        where (c.idCustomer == br.idCustomer
                               && br.idHardBox == p.idHardBox)).Distinct().ToList();

defaulterCustomers = from r in queryResult
                     select new ReportObject
                        {
                            CustomerId = r.idCustomer,
                            CustomerName = r.nameCustomer,
                            HardBoxDateRecord = r.idHardRecord,
                            PaymentDate = getLatestPaymentDate(r.idCustomer),
                        }).Distinct().ToList();

显然,我无权访问您的代码,所以请尝试一下并告诉我它是否适合您!你最终会得到一个内存列表

于 2013-03-08T16:04:33.723 回答