My requirement is to pass column name along with search data for dynamically searchning from db using linq query in entityframework.
[HttpGet]
public ActionResult Index(string Search, string Column)
{
if (!String.IsNullOrEmpty(Search))
{
List<Employee> result = new List<Employee>();
result = db.Employees.ToList();
result = result.Where(x => x.Column.ToLower().Contains(Search.ToLower())).ToList();
return View(result);
}
else
{
return View(db.Employees.ToList());
}
}
I think you are looking something like DLinq . Use the DLinq nuget package
var result= db.Employees.Where(Column+".Contains"+ "(\""+ Search.ToLower() + "\")").ToList();
The Column
parameter must match with the name of one string property in your Employee
entity. I suggest use a try-catch in case you pass a wrong name.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.