使用 Linq 实体在 JQuery Datepicker 捕获的两个日期之间创建一个 Select

Make a Select between two dates captured by JQuery Datepicker with Linq Entities

我使用的是 Database First、EF 6、Visual Studio 2013 Pro。

这是控制器上的代码

    public ActionResult Index(string searchString, string datepicker, string datepicker2)
    {
        var transactions = db.transactions.Include(t => t.Client).Include(t => t.DocumentType).Include(t => t.MovementType1);
        if (!String.IsNullOrEmpty(searchString))
        {
            transactions = transactions.Where(s => s.Client.Name.Contains(searchString) && 
                                              s.Date >= Convert.ToDateTime(datepicker) && s.Date < Convert.ToDateTime(datepicker2));     
        }
        return View(transaccions.ToList());
    }

按客户名称搜索时选得很好

这是视图的代码

@using (Html.BeginForm())
{
 <p>
    Search Transactions by Name: @Html.TextBox("SearchString") 
    First Date:  
                  <input type="text" id="datepicker"/>
    Second Date: 
                   <input type="text" id="datepicker2"/>

    <input type="submit" value="Search"/>
</p> 
}

如您所见,我通过用户在日期选择器中所做的选择来捕获日期。

我想执行此查询:SELECT * FROM TRANSACTION WHERE Client.Name LIKE (searchString) AND DATE BETWEEN (datepicker) AND (datepicker2)

我想按客户姓名和日期范围进行搜索。

问题是 Linq 不识别转换方法。

我一直在寻找一种方法,none 与我的代码兼容。

有什么建议吗?

您需要在 Linq 查询之外进行转换:

public ActionResult Index(string searchString, string datepicker, string datepicker2)
{
    var transactions = db.transactions.Include(t => t.Client).Include(t => t.DocumentType).Include(t => t.MovementType1);
    if (!String.IsNullOrEmpty(searchString))
    {
        //Get your dates here...
        var fromDate = Convert.ToDateTime(datepicker);
        var toDate = Convert.ToDateTime(datepicker2);

        transactions = transactions.Where(s => s.Client.Name.Contains(searchString) && 
                                          s.Date >= fromDate && s.Date < toDate);     
    }
    return View(transaccions.ToList());
}