在使用范围日期获取记录时,LINQ to Entities中只支持无参数构造函数和初始值设定项

本文关键字:构造函数 参数 支持 记录 获取 日期 使用范围 Entities to LINQ | 更新日期: 2023-09-27 17:59:42

我开发了一个小型web应用程序,因为我想在获取显示错误的记录时使用实体框架从数据库中获取记录。。

Only parameterless constructors and initializers are supported in LINQ to Entities. 
   public void Bindloanpayment()
    {
        int loanpaymentid = Convert.ToInt32(Session["Loanid"].ToString());
         int kr = 1998;
        int km = 05;
        int kdt = 02;
         var query = from p in mortgageentity.Payments
                        join D in mortgageentity.Debit_Method on p.Debit_Method_ID equals D.Debit_Method_ID
                        join pt in mortgageentity.Payment_Type on p.Payment_Type_ID equals pt.Payment_Type_ID
                     where (p.Client_Pmt_Date >= new DateTime(kr, km, kdt)) && (p.Client_Pmt_Date <= new DateTime(1999, 8, 1)) && (p.Loan_ID == loanpaymentid)
                        select new
                    {
                       Pmt_ID=p.Pmt_ID,
                       Loan_ID=p.Loan_ID,
                       Client_Pmt_Date=p.Client_Pmt_Date,
                       MtgSvr_Pmt_Start_Date2=p.MtgSvr_Pmt_Start_Date2,
                       Debit_Method_Desc=D.Debit_Method_Desc,
                       Total_Debit_Amt=p.Total_Debit_Amt,
                       CreditAmt=p.CreditAmt,
                       LenderAmt=p.LenderAmt,
                       Payment_Type_Desc=pt.Payment_Type_Desc,
                       Return_Code=p.Return_Code,
                       Returned_Date=p.Returned_Date
                    };
        grdPayments.DataSource = query.ToList();
        grdPayments.DataBind();
    }

请帮我怎么解决这个问题。。

在使用范围日期获取记录时,LINQ to Entities中只支持无参数构造函数和初始值设定项

您可以先尝试创建DateTime值:

DateTime start = new DateTime(kr, km, kdt);
DateTime end = new DateTime(1999, 8, 1);
...
// In the query
where p.Client_Pmt_Date >= start
   && p.Client_Pmt_Date <= end 
   && p.Loan_ID == loanpaymentid

这只是猜测出了什么问题,但似乎是可行的。。。