I have this search terms
[HttpPost]
public ActionResult Index(string searchString)
{
if (!String.IsNullOrEmpty(searchString))
{
students = st
Though it is a old question posting answer so that other will do it correctly
First thing to correct is we need to convert first integer to decimal and then search, assuming datatype for searchable column is also decimal
So first convert your search string to decimal
decimal dec;
bool IsDecimal = decimal.TryParse(searchString, out dec);
// Truncating search to 2 digits this should be equal to your db
// column precision so that rounding case are handled properly
decimal TruncateDigits = -1;
if (IsDecimal)
TruncateDigits = Convert.ToDecimal(Math.Truncate(dec * 100) / 100);
Now your search filed is same as your db coloumn precison and ready to get searched
students = students.Where(s => s.FIRST_NAME.Contains(searchString)
|| s.LAST_NAME.Contains(searchString)
|| s.PERSONAL_NUMBER.Contains(searchString)
|| s.ACD_UNI_DEGREES.DEGREE.Contains(searchString)
|| s.ACD_UNI_FACULTIES.FACULTY.Contains(searchString)
|| s.ACD_UNI_SPECIALIZATIONS.SPECIALIZATION.Contains(searchString)
|| (IsDecimal && (decimal.Round(s.SEMESTER, 2) == TruncateDigits))
Important point to note
(IsDecimal && (decimal.Round(s.SEMESTER, 2) == TruncateDigits))
Here again we will round to db column precision to handle rounding.
Also SqlFunctions.StringConvert will do rounding internally so if your number is db is 68.88 and your are searching for 68 it wont search because StringConvert will give you 69 (rounding) so if you want to search for 68 then you need to use Floor function
Something like
SqlFunctions.StringConvert(decimal.Floor(s.SEMESTER)).Contains(searchString)
Hope this helps !!!
You should ensure that you are using System.Data.Entity.SqlServer.SqlFunctions
. I rewrited your query and everything works fine.
I am facing the same problem right now with exactly the same principle you are aiming for. After a long search, I finally came to this:
[HttpPost]
public ActionResult Index(string searchString){
searchString = searchString.Replace(",", ".");
if (!String.IsNullOrEmpty(searchString)){
students = students.Where(s => s.FIRST_NAME.Contains(searchString)
|| s.LAST_NAME.Contains(searchString)
|| s.PERSONAL_NUMBER.Contains(searchString)
|| s.ACD_UNI_DEGREES.DEGREE.Contains(searchString)
|| s.ACD_UNI_FACULTIES.FACULTY.Contains(searchString)
|| s.ACD_UNI_SPECIALIZATIONS.SPECIALIZATION.Contains(searchString)
|| (s.SEMESTER.ToString()).Contains(searchString)
|| s.COR_PAYER_STATUS.NAME.Contains(searchString)
|| SqlFunctions.StringConvert(s.CREDIT_COUNT).Contains(searchString));
}
return View(students.ToList());
}
You are casting the decimal
to string
normally with ToString()
. However the bracetts are the magic weapon here:
(s.SEMESTER.ToString())
this makes a difference.
This syntax will allow you to search with the notation .
(Example: 1203.4 instead of 1203,4). Which is not really user-friendly.
To solve this, use the .Replace(',', '.')
on your searchString. This will apply the trick on background and no one will notice.
I hope it helped anyone struggling with the same problem.