仅在不是空值时搜索

时间:2015-06-03 19:58:33

标签: c# asp.net-mvc linq if-statement

我目前正在编写搜索功能,以便通过故障单所有者,故障单技术,状态等来搜索故障单。但是我想我可以在Linq语句测试中间输入if语句,如果不是null,不幸的是我是在LINQ语句中的if语句中出错。这是我的控制器代码:

<div class="row-fluid">
    <div class="span6">
        @Html.LabelFor(m => m.Id)
        @Html.TextBoxFor(m => m.Id)
        @Html.ValidationMessageFor(m => m.Id)

        @Html.LabelFor(m => m.Username)
        @Html.TextBoxFor(m => m.Username)
        @Html.ValidationMessageFor(m => m.Username)

        @Html.LabelFor(m => m.FirstName)
        @Html.TextBoxFor(m => m.FirstName)
        @Html.ValidationMessageFor(m => m.FirstName)

        @Html.LabelFor(m => m.LastName)
        @Html.TextBoxFor(m => m.LastName)
        @Html.ValidationMessageFor(m => m.LastName)
    </div>
    <div class="span6">
        @Html.LabelFor(m => m.Email)
        @Html.TextBoxFor(m => m.Email)
        @Html.ValidationMessageFor(m => m.Email)

        @Html.LabelFor(m => m.UserTypeId)
        @Html.TextBoxFor(m => m.UserTypeId)
        @Html.ValidationMessageFor(m => m.UserTypeId)

        @Html.LabelFor(m => m.Created)
        @Html.TextBoxFor(m => m.Created)
        @Html.ValidationMessageFor(m => m.Created)

        @Html.LabelFor(m => m.Active)
        @Html.CheckBoxFor(m => m.Active)
        @Html.ValidationMessageFor(m => m.Active)

    </div>
</div>

1 个答案:

答案 0 :(得分:3)

关闭。您只需添加几个分号和model=model s。

var model = db.Tickets
  .Include(t => t.TicketNotes);
if (searchTickets.TechnicianId != null)
{
  model=model.Where(t => t.TechnicianId==g);
}
model=model.Where(t => t.TicketStatusId==new Guid("553F4C93-4A72-44BD-A9CE-FAB4F87D4E08"))
  .OrderBy(t => t.OpenDate);
var result=model.ToList();