Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用c在两个日期之间进行搜索#_C# - Fatal编程技术网

C# 如何使用c在两个日期之间进行搜索#

C# 如何使用c在两个日期之间进行搜索#,c#,C#,我在MVC应用程序中创建了一个非常简单的搜索表单,允许用户搜索字符串并返回结果。我想让我的用户在两个日期之间进行搜索,但我不确定该如何编程 这是到目前为止我的代码。您会注意到,这仅用于搜索作为参数传递到控制器中的字符串。我已经开始为日期选择器编写代码,但是我被卡住了 查看 <h2>Search</h2> @using (Ajax.BeginForm("Search", "Search", new AjaxOptions { OnSuccess = "Success

我在MVC应用程序中创建了一个非常简单的搜索表单,允许用户搜索字符串并返回结果。我想让我的用户在两个日期之间进行搜索,但我不确定该如何编程

这是到目前为止我的代码。您会注意到,这仅用于搜索作为参数传递到控制器中的字符串。我已经开始为日期选择器编写代码,但是我被卡住了

查看

<h2>Search</h2>
@using (Ajax.BeginForm("Search", "Search", new AjaxOptions
{
    OnSuccess = "Success",
    OnFailure = "Failure",
    UpdateTargetId = "test",
    InsertionMode = InsertionMode.Replace
}))
{
<table class="table">
    <tr>
        <td><label>Vessel Name</label></td>
        <td>@Html.TextBox("term",null, new { @class = "k-textbox" })</td>
    </tr>
    <tr>
        <td>
            <label>From</label>
            @(Html.Kendo().DatePicker()
                .Name("date_from")
                .Value(DateTime.Today)
                )
        </td>
        <td>
            <label>To</label>
            @(Html.Kendo().DatePicker()
                .Name("date_to")
                .Value(DateTime.Today)        
            )
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Search" />
        </td>
    </tr>         
</table>    
}

<table class="table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Location</th>
            <th>MMSI</th>
            <th>Created</th>
        </tr>
    </thead>
    <tbody id="test">
        @Html.Partial("_results")     
    </tbody>
</table>
局部视图 因为我使用ajax更新目标ID,所以我使用“替换”插入方法以局部视图的形式返回结果

@model IEnumerable<Multiple_Table_Post.Models.vessel>
@foreach (var item in Model)
{
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.vessel_name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.vessel_location)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.vessel_mmsi)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.created)
    </td>
</tr>
}
@model IEnumerable
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.Vesser\u name)
@DisplayFor(modelItem=>item.Vesser\u位置)
@DisplayFor(modelItem=>item.Vesser\u mmsi)
@DisplayFor(modelItem=>item.created)
}
你会注意到,我已经开始设置日期选择器,但我不知道如何编写c,以便在它们之间进行搜索。我将它们作为datetime参数传递到方法中,但之后我就完全陷入了困境


非常感谢

您可以在所显示的任一LINQ查询中执行此操作

var vessels= (from o in db.vessels
where (o.created>= date_from && o.created<= date_to)
select o);//I have not tested this.
var Vessers=(从0到db.Vessers
其中(o.created>=日期\u从&o.created
var-vessers=(从db.vessers中的o开始
其中o.vessel\u date=>date\u from
&&o.船舶日期=<日期至
&&(术语==null)
||o.船舶名称包含(术语)
&&o.容器位置包含(术语)
&&o.船舶(包括(期限))
选择o);

你能把所有那些吓人的东西(例如)都添加为标签吗?标题听起来很简单,但对我来说不合适。谢谢,这其实很好、干净、简单。我把它复杂化了。这很好用。
var vessels= (from o in db.vessels
where (o.created>= date_from && o.created<= date_to)
select o);//I have not tested this.
var vessels = (from o in db.vessels
               where    o.vessel_date => date_from
                     && o.vessel_date =< date_to
                     && (    term==null 
                          ||    o.vessel_name.Contains(term)
                             && o.vessel_location.Contains(term)
                             && o.vessel_mmsi.Contains(term))
               select o);