2016-04-27 90 views
2

我有一个变量,它在一天,一周和一个月之间有所不同。一个表单将这个信息发送回控制器。不过,我试图在下面使用这种方法来获得“在1周内查看订单”。没有错误,它根本没有显示任何订单。C#比较OrderDate与变量

Controller; (Just trying to get it to work before i change the amount of days) 
else if (date == "day") 
      { 
       DateTime today = DateTime.Now; 
       DateTime answer = today.AddDays(10); 

       return View(db.Orders.Where(x => x.OrderDate <= answer).ToList()); 
      } 

这是我的观点,包括我使用的其他部分;

@using (Html.BeginForm("Index", "EditOrders", FormMethod.Get)) 
{ 
    //the following are search options 

    <b> Search for: </b>@Html.RadioButton("option", "FirstName") <text>First Name</text> @Html.RadioButton("option", "PostalCode") <text> Post-Code </text> 
    @Html.RadioButton("option", "Username")<text>Email-Address</text> @Html.TextBox("search") 
    <text>Orders Made </text><select id="date" name="date"> 
     <option value="none">No day</option> 
     <option value="day">In the last day</option> 
     <option value="week">In the last week</option> 
     <option value="month">In the last month</option> 
     <option value="year">In the last year</option> 
    </select> 
    <input type="submit" name="submit" value="Search" /> 
} 
+0

要确认(可能明显) - 什么是类型订单日期在db.Orders中。另外要确认,你问的是**周**,但检查** ==“天”**,然后添加10天 - 这是一个错误的问题,或者你的代码检查一天两次,而不是周? –

+0

看看这个答案,并输出生成的SQL - 应该帮助你找到问题所在:http://stackoverflow.com/questions/3920990/best-way-to-show-the-sql-trace-of- a-linq-query-to-entity-framework-3-5 –

+0

@ freedomn -m它是一个DateTime,是的只是为了测试目的,如果我可以得到它来检查10天,我可以得到它来检查任何。在代码中,它今天设置,然后将这些日子添加到今天。我需要它来显示订单日期介于“今天”和“回答”之间的订单 –

回答

0

我的代码是正确的,现在的工作,我所要做的就是添加一个 - 在添加天望过去

DateTime today = DateTime.Now; 
       DateTime answer = today.AddDays(-10);