Adalat  Khan

Adalat Khan

  • 958
  • 1.4k
  • 644.7k

Linq Query Issues

May 28 2021 11:59 AM
Hi,
I need a Linq query that produces the following result:
i have a database table EmplyeesDetail. I performed search on different fields such as by Name, by Joining Date and by Department. Now i have issues in search by Department because for Departments i have an Enumeration where i defined all the departments and i am storing the integer value of the department instead of department name but in the search field a user do not know the department enum integer value but user enters the department name in the search textbox. Now how i can display records from the table EmployeesDetail using the Department name because i am not storing the department name but i am storing the integer value of the enum but user enters the department in the search text box. Following is my Action Method:
 
public async Task EmployeeList(string SearchBy, string txtSearch)
{
if (SearchBy == "EmpCode")
{
var empList = await _employeesContext.EmployeesDetail.Where(x => x.EmployeeCode == Convert.ToInt32(txtSearch) || txtSearch == null).ToListAsync();
return View(empList);
}
else
if(SearchBy == "Name")
{
var empList = await _employeesContext.EmployeesDetail.Where(x => x.EmployeeFirstName.StartsWith(txtSearch) || txtSearch == null).ToListAsync();
return View(empList);
}
else
if (SearchBy == "Department")
{
var empList = await _employeesContext.EmployeesDetail.Where(x => x.EmployeeDepartment == txtSearch ) || txtSearch == null).ToListAsync();
return View(empList);
}
}
 
The first parameter of the action method represents the radio button name and the second parameter of the action method represents the textbox in which the user enters the search text.
 
Please check the following part of SearchBy == "Department":
Where(x => x.EmployeeDepartment == txtSearch ) 
 
The txtSearch contains the department string value but in database table EmployeeDepartment contains the integer key values of enumeration so how to perform this Search???
 
Following is the Departments Enumeration
 
public enum Departments
{
Administration,
HR,
Finance,
IT 
 

Answers (2)