Entity framework Linq to实体无法识别方法System.DateTime。。无法将其转换为存储表达式
我有一个问题花了我几个星期才解决,但我一直无法解决 我有一个类,我有两种方法。以下内容应该是数据库中的最新日期。该日期表示客户对某件物品的最新付款:Entity framework Linq to实体无法识别方法System.DateTime。。无法将其转换为存储表达式,entity-framework,linq-to-entities,Entity Framework,Linq To Entities,我有一个问题花了我几个星期才解决,但我一直无法解决 我有一个类,我有两种方法。以下内容应该是数据库中的最新日期。该日期表示客户对某件物品的最新付款: public DateTime getLatestPaymentDate(int? idCustomer) { DateTime lastDate; lastDate = (from fp in ge.Payments from cst in ge.Customers
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实体无法识别方法System.DateTime。。无法将其转换为存储表达式
有谁能提供有用的信息让我远离这个混乱的错误?任何帮助都将不胜感激
非常感谢 看看这些其他问题: 基本上,不能在C端使用值并将其转换为SQL。第一个问题提供了更彻底的解释;第二种方法为您的问题提供了一个简单的解决方案 编辑: 简单地说:EF要求SQL server执行getLatestPaymentDate方法,对此它没有任何线索。您需要在程序端执行它 只需先执行查询,将结果放入列表,然后在内存列表中进行选择:
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();
显然,我无法访问您的代码,所以请尝试一下,并告诉我它是否适合您!
您将得到一个内存中的列表您好,谢谢您的输入,但我确实还无法实现这一点。您说过您提供的第二个问题是最简单的,但我不知道它实际上如何解决我的问题,因为我已经声明了DateTime类型的变量,并将其解析为假设的日期,但仍然不起作用。你能再给我解释一下吗?我会非常感激的!当然简单地说:getLatestPaymentDate没有SQL等价物。我会修改我的答案,让你能做的更加明确。