search decimal values in Linq query

后端 未结 3 1177
独厮守ぢ
独厮守ぢ 2021-01-16 09:33

I have this search terms

[HttpPost]
public ActionResult Index(string searchString)
{
    if (!String.IsNullOrEmpty(searchString))
    {
        students = st         


        
3条回答
  •  死守一世寂寞
    2021-01-16 10:11

    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.

提交回复
热议问题