本文介绍了在Linq查询中搜索十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个搜索字词
[HttpPost]
public ActionResult索引(string searchString)
{
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)
|| SqlFunctions.StringConvert(s.SEMESTER).Contains(searchString)
|| s.COR_PAYER_STATUS.NAME.Contains (searchString)
|| SqlFunctions.StringConvert(s.CREDIT_COUNT).Contains(searchString));
}
return View(students.ToList());
}
但调试时会抛出异常:
问题在这里:
SqlFunctions.StringConvert(s.SEMESTER).Contains(searchString)
SEMESTER 是十进制,而 是字符串。
我该如何改善?您应该确保使用 System.Data.Entity.SqlServer.SqlFunctions
。我重写你的查询,一切正常。
I have this search terms
[HttpPost]
public ActionResult Index(string searchString)
{
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)
|| SqlFunctions.StringConvert(s.SEMESTER).Contains(searchString)
|| s.COR_PAYER_STATUS.NAME.Contains(searchString)
|| SqlFunctions.StringConvert(s.CREDIT_COUNT).Contains(searchString));
}
return View(students.ToList());
}
but on debugging it throws an exception:
The problem is here:
SqlFunctions.StringConvert(s.SEMESTER).Contains(searchString)
SEMESTER is decimal and searchString is string.How can I improve that?
解决方案
You should ensure that you are using System.Data.Entity.SqlServer.SqlFunctions
. I rewrited your query and everything works fine.
这篇关于在Linq查询中搜索十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!