I need to search and find the exact term in my ASP.NET core application , I am using now .Contains but its show all the terms contains what I type and output not correct I need to find only what I type in the search field ,
This is my DB Repository code :
public List<LabResult> Search(string term)
{
return db.LabResults.Where(a => a.PatientNo.ToString().Contains(term)).ToList();
}
For example patient no = 250
I need to find only data belongs to this patient no 250
not 2250 or 3250
what I need to use instead of .Contains ?
CodePudding user response:
==
should do the trick:
public List<LabResult> Search(string term)
{
return db.LabResults
.Where(a => a.PatientNo.ToString() == term)
.ToList();
}
Note that if PatientNo
is a number you should transform term
to number and not perform this transform vise versa in the database. Something along this lines (assuming int
for PatientNo
):
public List<LabResult> Search(string term)
{
if(int.TryParse(term, out var searchNo))
{
return db.LabResults
.Where(a => a.PatientNo.ToString() == term)
.ToList();
}
else
{
// or throw
return new List<LabResult>();
}
}