2013-03-08 28 views
3

我有一个问题需要花费数周才能解决,而我一直无法解决。Linq to Entities不能识别方法System.DateTime ..并且无法将其转换为商店表达式

我有一个类,我有两种方法。以下是应该从数据库中获取最新日期。这个日期代表客户做了以“东西”最新的支付:

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报告:

//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

请有人提供一个有用的输入,使我从这个凌乱的错误?任何帮助将不胜感激 !

非常感谢!

回答

1

看一看这些问题:

LINQ to Entities does not recognize the method

LINQ to Entities does not recognize the method 'System.DateTime Parse(System.String)' method

基本上,你不能在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(); 

我没有访问到你的代码,很明显,所以试试吧出来告诉我它是否适合你! 你最终会得到一个内存列表

+0

嗨,感谢您的输入,但我真的无法完成这项工作。你说你提供的第二个问题是最简单的,但是我不明白它是如何解决我的问题的,因为我已经声明了一个DateTime类型的变量并将其解析为假设的日期,但仍然不起作用。你能再解释一下吗?我将非常感激! – 2013-03-14 22:26:58

+0

请检查我上面的评论。非常感谢! – 2013-03-15 13:47:59

+0

当然!简单地说:'getLatestPaymentDate'没有SQL等价物。我会编辑我的答案,以便更明确地说明你可以做什么。 – SolarBear 2013-03-15 14:38:25

相关问题