如何按2个字段过滤搜索(例如姓名和日期) [英] How to filter search by 2 fields(eg. Name and date)
问题描述
我正在尝试按照为记录创建的顾问名称和日期来过滤我的搜索。我正在使用日期选择器作为我的日历,并使用下拉列表来选择顾问名称。目前我的搜索工作只是顾问,我无法弄清楚如何将日期添加为过滤器。
I am trying to filter my search by consultant name and date created for records. i am using a datepicker for my calendar and a drop down list for selection on the consultant name. At the moment I have the search working for just the consultant, i cannot figure out how to add the date as a filter.
[HttpPost]
public ActionResult TimesheetHistroy(Timesheet timesheet)
{
ViewBag.ConsultantNum = new SelectList(db.Consultants, "ConsultantNum", "FullName", timesheet.ConsultantNum);
var timesheets = db.Timesheets.Include(t => t.Client).Include(t => t.Consultant).Where(a=>a.ConsultantNum==timesheet.ConsultantNum && EnterDate == timesheet.CaptureDate) ;
var r = timesheets.ToList();
return View(r);
}
我的尝试:
我曾尝试独立使用它,但我需要它们一起工作
What I have tried:
I have tried using it independently, but i need them to work together
推荐答案
我假设 a.EnterDate 是DateTime或Date类型, timesheet.CaptureDate 是由datepicker插件返回的字符串
你没有提到问题是什么是。我进一步假设代码丢错了???因为代码试图比较一个字符串和一个日期?
有几件事要看。
1.日期选择日期格式。请确保您将其编码为返回日期格式,如05/28/2017或要求是。
I'm assuming a.EnterDate is a DateTime or Date type, timesheet.CaptureDate is the string return by the datepicker plugin
You didn't mention what the issue was. I further assume that the code threw error??? because the code is trying to compare a string and a Date?
There are couple of things to look at.
1. The datepicker date format. Make sure you coded it to return date format like 05/28/2017 or however the requirements are.
( function (){
(' #CaptureDate')。datepicker({
dateFormat:' mm / dd / yy' // 以某种方式返回05/28/2017 vs 05/28/17
});
});
('#CaptureDate').datepicker({ dateFormat: 'mm/dd/yy' //somehow this return 05/28/2017 vs 05/28/17 }); });
2.这是其中一个选择。将EnterDate转换为格式为mm / dd / yyyy的字符串,并将其与CaptureDate进行比较。同样,我进一步假设EnterDate不允许null。如果是,请更改代码以使月份为a.EnterDate.Value.Month
2. Here is one of the option. Convert the EnterDate into string with format mm/dd/yyyy and compare it with CaptureDate. Again, I further assumed that the EnterDate does not allow null. If it does, change the code to get the month to "a.EnterDate.Value.Month"
var timesheets = db.Timesheets.Include(t => t.Client).Include(
t => t.Consultant).Where(a => a.ConsultantNum == timesheet.ConsultantNum &&
SqlFunctions.Replicate("", 2 - SqlFunctions.StringConvert((double)a.EnterDate.Month).TrimStart().Length) +
SqlFunctions.Replicate("0", 2 - SqlFunctions.StringConvert((double)a.EnterDate.Month).TrimStart().Length) +
SqlFunctions.StringConvert((double)a.EnterDate.Month).TrimStart() +
SqlFunctions.Replicate("/", 2 - SqlFunctions.StringConvert((double)a.EnterDate.Month).TrimStart().Length) +
SqlFunctions.Replicate("0", 2 - SqlFunctions.DateName("dd", a.EnterDate).Trim().Length) +
SqlFunctions.DateName("dd", a.EnterDate).Trim() + "/" +
SqlFunctions.DateName("year", a.EnterDate) == timesheet.CaptureDate).ToList();
这篇关于如何按2个字段过滤搜索(例如姓名和日期)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!