In my ASP.NET MVC application, I want to search and get data from the database once users search the value. Example User type BCD-1254
from the view. In Database, the record could be saved in different ways like, WPBCD-1254
, BCD1254
, WPBCD-1254
, BCD-1254
, bcd-1254
, ''bcd1254`` wise. So from the controller, I want to select all the records related to the search value.
I did this way, but from the search results, not all records are getting from the database table. How can I search for it from the controller?
List<ServiceHistoryVM> History = new List<ServiceHistoryVM>();
var Services = (from a in db.AppRequest
join v in db.VehicleService on a.Id equals v.Req_Id
join vd in db.VehicleServiceDetails on v.Id equals vd.VehicleService_Id
where vd.Vehicle_No.ToLower() == VehicleNumber.ToLower()
select new ServiceHistoryVM
{
Date = a.Created_Date,
MileageToDate = vd.Current_Service_Mileage.ToString(),
MileageToLast = vd.Last_Service_Mileage.ToString()
}
).ToList();
CodePudding user response:
Let me try to answer your question. If your main focus is to get or return all matching records that contain what is entered or similar to what is entered then you will need a simple algorithm to tweak your search criteria.
char[] sch =new char[]{'-','/','*'};
string[] searchwords = searchcriteria.Split(sch);
string keyword="";
foreach(var word in searchwords){
keyword=keyword word;
}
Your linq lambda query will now look like below:
var searchresult = FROM s in db.Table WHERE s.FIELD.Contains(searchcriteria) || s.FIELD.Contains(keyword) SELECT s;
This is a scenario where you may need to replace many characters in the search with empty space. The searchcriteria is the variable for the search string.