我正在运行查询,并且有多个参数可用于过滤返回的记录。参数来自下拉菜单,因此如果用户没有选择选项,则参数将以空字符串形式出现。
如何忽略""?
的参数以下是查询:(在LINQ中可能有更好的方法可以做到这一点,但我的SQL技能更强)。我正在MVC 5 / C#中构建此报告。
approvalReport = (from td in db.TData
from call in
(from x in db.CallLog
where x.TID == td.TID
orderby x.CallDate descending
select new
{
x.CallStatus,
x.CallNotes,
x.CallDate
}).Take(1).DefaultIfEmpty()
from appt in
(from x in db.Appt
where x.TID == td.TID
orderby x.ApptCreateDate descending
select new
{
x.ApptStatus,
x.ApptDate
}).Take(1).DefaultIfEmpty()
where (td.Location == location && td.ApprovedBy == approvedBy && td.PCP == pcp && td.IPA == ipa)
orderby td.MemberName descending
select new ApprovalDataVM()
{
MemberName = td.MemberName,
PCP = td.PCP,
IPA = td.IPA,
Location = td.Location,
ApprovedBy = td.ApprovedBy,
CallStatus = call.CallStatus,
ApptDate = appt.ApptDate,
ApptStatus = appt.ApptStatus,
callLogCt = db.CallLog.Where(x => x.TID == td.TID).Count(),
});
我已经对此做了一些研究并尝试了where子句,但它没有用。该报告没有提取任何数据。以下示例缩写为3个参数,并且" approvedBy"将永远不会为null或""。
where (td.ApprovedBy == approvedBy && (pcp =! "' && td.PCP == pcp) && (ipa != "" && td.IPA == ipa)
我不想为所有可能的参数组合重复自己。对此最好的解决方案是什么?
CallLog和Appt表具有td.TID的foriegn键。输出将是显示视图模型中所有数据的列表。
查看:
<div class="table-responsive">
<table class="table table-hover small">
<tr>
<th></th>
<th>Approved By</th>
<th>Member Name</th>
<th>DOB</th>
<th>PCP</th>
<th>IPA</th>
<th>Call Status</th>
<th>Call Count</th>
<th>Appt Date</th>
<th>DOS</th>
</tr>
@{ int counter = 0; }
@foreach (var item in Model.reportData)
{
counter = counter + 1;
<tr>
<td>
@counter
</td>
<td>
@Html.DisplayFor(modelItem => item.ApprovedBy)
</td>
<td>
@Html.DisplayFor(modelItem => item.MemberName)
</td>
<td>
@if (item.DateOfBirth != null)
{
@item.DateOfBirth.Value.ToShortDateString()
}
</td>
<td>
@Html.DisplayFor(modelItem => item.PCP)
</td>
<td>
@Html.DisplayFor(modelItem => item.IPA)
</td>
<td>@Html.DisplayFor(modelItem => item.CallStatus)</td>
<td>@Html.DisplayFor(modelItem => item.callLogCt)</td>
<td>@Html.DisplayFor(modelItem => item.ApptStatus)</td>
<td>
@if (item.ApptDate != null)
{
@item.ApptDate.Value.ToShortDateString()
}
</td>
</tr>
}
</table>
提供搜索查询的表单:
<form class="form-inline" role="form">
<div class="form-group" style="padding-right:15px;">
<label class="control-label">*Approved By: </label>
@Html.DropDownList("approvedBy", Model.ApprovedByList, "Select One", new { @class = "input-sm" })
@Html.ValidationMessageFor(model => model.ApprovedByList, "", new { @class = "text-danger" })
</div>
<div class="form-group" style="padding-right:15px;">
<label class="control-label">*Location: </label>
@Html.DropDownList("location", (IEnumerable<SelectListItem>)ViewBag.location, "Select One", new { @class = "input-sm" })
</div>
<input type="submit" value="Run Report" class="btn-primary btn-sm" />
<hr />
<div class="form-group" style="padding-right:15px;">
<label class="control-label">PCP: </label>
@Html.DropDownList("pcp", Model.PCPList, "All", new { @class = "input-sm" })
</div>
<div class="form-group" style="padding-right:15px;">
<label class="control-label">IPA: </label>
@Html.DropDownList("ipa", Model.IPAList, "All", new { @class = "input-sm" })
</div>
<div class="form-group" style="padding-right:15px;">
<label class="control-label">Call Status: </label>
@Html.DropDownList("callStatus", Model.CallStatusList, "All", new { @class = "input-sm" })
</div>
<br /><br />
</form>
答案 0 :(得分:2)
我认为你真的希望这能得到你想要的结果。我的假设是,如果用户将参数留空,则希望该参数与所有行匹配,换句话说,不要按该参数进行过滤。
td.ApprovedBy == approvedBy && (pcp = "" || td.PCP == pcp) && (ipa == "" || td.IPA == ipa)
答案 1 :(得分:2)
你可以这样做
where (td.ApprovedBy == approvedBy && td.Location == location && (pcp == "" || td.PCP == pcp) && (ipa == "" || td.IPA == ipa))