【问题标题】:how to get records from database based on date range in asp.net linq如何根据asp.net linq中的日期范围从数据库中获取记录
【发布时间】:2011-06-21 07:51:23
【问题描述】:

我已经开发了一个小型 Web 应用程序,我正在使用实体框架工作。我想根据日期范围获取记录并在 gridview 中绑定该数据。如何使用 linq 编写查询。请帮助我。 .在这里我发布了我的代码,我试图获取记录......

       var query = from p in entity.Payments
                    join D in entity.Debit_Method on p.Debit_Method_ID equals D.Debit_Method_ID
                    join pt in entity.Payment_Type on p.Payment_Type_ID equals pt.Payment_Type_ID
                    where p.Client_Pmt_Date >='1998-12-01' && p.Client_Pmt_Date<='1999-08-01' && p.Loan_ID=loanid
                    select new
                    {
                        p.Pmt_ID,
                        p.Loan_ID,
                        p.Client_Pmt_Date,
                        p.MtgSvr_Pmt_Start_Date2,
                        D.Debit_Method_Desc,
                        p.Total_Debit_Amt,
                        p.CreditAmt,
                        p.LenderAmt,
                        pt.Payment_Type_Desc,
                        p.Return_Code,
                        p.Returned_Date
                        //p.Pmt_ID,
                        // D.Debit_Method_Desc,
                        // pt.Payment_Type_Desc,
                        // p.Client_Pmt_Date,
                        // p.MtgSvr_Pmt_Start_Date2,
                        // p.Amt,
                        // p.CreditAmt,
                        // p.Loan_ID,
                        // p.Pmt_Comments

                        // p.Loan_ID,
                    };
        grdPayments.DataSource = query.ToList();
        grdPayments.DataBind();

    }

【问题讨论】:

    标签: asp.net linq-to-sql c#-4.0


    【解决方案1】:

    您需要将 Date 与 DateTime 对象而不是字符串进行比较。这是一个带有 POCO(普通旧 CLR 对象)的 LINQ 示例:

    var dates = new List<DateTime> { new DateTime(2011, 1, 1), new DateTime(2010, 1, 1), new DateTime(2009, 1, 1) };
    var result1 = from x in dates where x < new DateTime(2011, 1, 1) && x > new DateTime(2009,1,1) select x;
    var result2 = dates.Where(x => x < new DateTime(2011, 1, 1) && x > new DateTime(2009,1,1));
    

    【讨论】:

    • 您好,感谢您的回复..我想使用连接获取记录,我该如何编写查询?请您帮帮我。
    • 您需要将 >='1998-12-01' 替换为 >= new DateTime(1998, 12, 1)
    • 我尝试用 replace new DateTime(1998, 12, 1) 编写代码,但这里不接受 and(&&) ....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多