title: how to filter between dates in ASP.NET Web Application
body: <p>I am very new to MVC and software development, and I want to implement a search by date functionality within my application. On the index view I want users to be able to search by two date fields that are available on the web page, "Date" and "Order Date". Currently this is the code I have in the view, it basically specifies the layout of the page.</p>
  
  <pre><code>using (Html.BeginForm("Index","PurchaseOrder",FormMethod.Get))
  {
      &lt;p&gt;
          Search by Date: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;@Html.TextBox("searchBy","dateSearchBegin") to: @Html.TextBox("searchBy","dateSearchEnd")
          &lt;input type="submit" value="Search" /&gt; &lt;br /&gt;&lt;br /&gt;
          Search by Order Date: &amp;nbsp;@Html.TextBox("searchBy","dateOrderedBegin") to: @Html.TextBox("searchBy","dateOrderedEnd")
          &lt;input type="submit"  value="Search" /&gt;
          &lt;br /&gt; &lt;br /&gt;
          Search by: @Html.RadioButton("searchBy","Requestor", true) Requestor
          @Html.RadioButton("searchBy","Vendor") Vendor
          @Html.RadioButton("searchBy","workOrder") Work Order # &lt;br /&gt;
          @Html.TextBox("search","", new { style = "width:10000px" })
          &lt;input type="submit" value="Search"  /&gt;
      &lt;/p&gt;
  
  }
  </code></pre>
  
  <p>In the controller, I have this code. This is where I am trying to provide the logic to be able to filter by different fields.</p>
  
  <pre><code>// GET: PurchaseOrder
  public ActionResult Index(string searchBy, string search,  int? page)
  {
  
      if (searchBy == "Requestor")
      {
          return View(db.PurchaseOrders.Where(x =&gt; x.Requestor.ToUpper().Contains(search)).OrderBy(i =&gt; i.DateOrdered).ToPagedList(page ?? 1, 15));
      }
      else if (searchBy == "Vendor")
      {
          return View(db.PurchaseOrders.Where(x =&gt; x.Vendor.ToUpper().Contains(search)).OrderBy(i=&gt;i.DateOrdered).ToPagedList(page??1,15));
      }
      else if (searchBy == "workOrder")
      {
          return View(db.PurchaseOrders.Where(x =&gt; x.PurchaseRequest_.ToUpper().Contains(search)).OrderBy(i =&gt; i.DateOrdered).ToPagedList(page ?? 1, 15));
      }
      else if (searchBy == "dateSearchBegin")
      {
          return View(db.PurchaseOrders.Where(x =&gt; x.Date &gt; "dateSearchBegin" &amp;&amp; x.Date &lt; "dateSearchEnd").OrderBy(i =&gt; i.Date).ToPagedList(page ?? 1,15));
      }
      else if (searchBy == "dateSearchEnd")
      {
          return View(db.PurchaseOrders.Where(x =&gt; x.Date &gt; "dateOrderedBegin" &amp;&amp; x.Date &lt; "dateOrderedEnd").OrderBy(i =&gt; i.Date).ToPagedList(page ?? 1,15));
      }
      int pageSize = 15;
      int pageNumber = (page ?? 1);
      pageNumber = page == null ? (ViewBag.pageData == null ? 1 : (int)ViewBag.pageData) : page.Value;
      ViewBag.pageData = page;
      return View(db.PurchaseOrders.OrderBy(i =&gt; i.DateOrdered).ToPagedList(pageNumber, pageSize));
  }
  </code></pre>
  
  <p>I am getting an error message  that states "operator '&lt;' cannot be applied to operands of type 'System.DateTime?' and 'string'. How can I fix this error, and is there a more efficient way to code this to achieve what I am trying to do?</p>