我试图从用户那里获取输入日期,将日期与文本文件中的数据进行比较(在对象列表中),并显示所有早于输入日期的条目。
文本文件:
John,Smith,02/05 / 1969,1,700000,Manager,None
简,母鹿,1977年4月1日,2,600000,雇员,约翰
Jim,Bean,1985年11月11日,3,650000,员工,Jane
罗杰,威尔科,1990年12月19日,2000年4月,实习生,简(Jane)
Susan,Roe,1995年6月22日,5,180000,Jane培训生
控制器:
namespace EpiUseTechAssessment.Controllers
{
public class HRManagerController : Controller
{
List<EmployeeViewModel> employees = new List<EmployeeViewModel>();
// GET: HRManager
public ActionResult HRManager()
{
List<EmployeeViewModel> employees= GetData();
return View(employees);
}
private List<EmployeeViewModel> GetData()
{
string[] Lines;
string filepath = @"D:Employees.txt";
//Read textfile
Lines = System.IO.File.ReadAllLines(filepath);
//Seperate and assign
foreach (string line in Lines)
{
employees.Add(ParseEmployee(line));
}
return (employees);
}
private EmployeeViewModel ParseEmployee(string line)
{
string[] L = line.Split(',');
string Name = L[0];
string Surname = L[1];
DateTime Birthdate = DateTime.ParseExact(L[2], "dd/MM/yyyy", CultureInfo.InvariantCulture);
int EmpNum = Convert.ToInt32(L[3]);
int Salary = Convert.ToInt32(L[4]);
string Role = L[5];
string Reports = L[6];
return new EmployeeViewModel(Name, Surname, Birthdate, EmpNum,
Salary, Role, Reports);
}
//Search by Employee Name
public ActionResult SearchEmpDetails(string empname)
{
GetData();
var emp = from s in employees select s;
if (!String.IsNullOrEmpty(empname))
{
emp = employees.Where(z => z._Name.Contains(empname));
}
return View("HRManager",emp.ToList());
}
//Employees older then a date
public ActionResult SearchOlderDate(string day, string month, string year)
{
GetData();
DateTime inputdate = DateTime.Parse(year+"/"+ month +"/"+ day);
//var empdates = employees.Where(e => e._Birthdate == inputdate);
foreach (EmployeeViewModel item in employees)
{
var compdate = item._Birthdate;
int result = DateTime.Compare(inputdate, compdate);
if (result != 0)
{
employees.Add(item);
}
}
return View("HRManager", employees.ToList());
}
}
}