MVC SQLQuery将数据库日期与当前日期进行比较

时间:2013-10-19 15:27:52

标签: c# .net asp.net-mvc database entity-framework-4

我一直在尝试获取列表中的记录列表,其中列调度>今天(当前日期+时间)

var billPays = from s in db.BillPays.SqlQuery("SELECT * FROM BillPay WHERE " + 1 + " = Active AND "  + DateTime.Now + " > ScheduleDate").ToList()
            select new BillPayModel
            {
                //model             
            };

             return View(billPays);

不幸的是,这并没有返回任何东西。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

var billPays = from s in db.BillPays.SqlQuery("SELECT * FROM BillPay WHERE '"+ DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")+"' > ScheduleDate").ToList()

select new BillPayModel
    {
            //model             
    };

答案 1 :(得分:0)

string myQuery = string.Format("SELECT * FROM BillPay WHERE 1 = Active AND {0:s} > ScheduleDate", DateTime.now);
var billPays = from s in db.BillPays.SqlQuery(myQuery).ToList();
select new BillPayModel
        {
            //model             
        };

         return View(billPays);