问题描述
我有一个产品表,其中的标题为字符串.
i have a product table which has a Title as string.
我认为我有
@using (Html.BeginForm("Serach","Store"))
{
<input type="text" name="q" class="searchbox_textbox" />
<input type="submit" class="searchbox_btn" />
}
我的控制器内有
public ActionResult Serach(string q)
{
var result = storeDB.Products
.Where(p => p.Title.Contains(q) || string.IsNullOrEmpty(q));
return View(result);
}
当我运行页面并输入单词进行搜索时,出现此错误
when i run the page and type a word to search for it give me this error
异常详细信息:System.Data.SqlServerCe.SqlCeException:该函数的指定参数值无效. [参数#= 1,函数名称(如果已知)= isnull]
Exception Details: System.Data.SqlServerCe.SqlCeException: The specified argument value for the function is not valid. [ Argument # = 1,Name of function(if known) = isnull ]
出什么问题了?如果我想向用户显示一条告诉他们您的搜索结果的消息,该怎么办没有任何商品
whats the problem? and what should i do if i want to show users a meesage that tell them your serachdidn't match any product
推荐答案
好吧,您应该使用一些日志记录来查找实际发送到数据库的内容-但就我个人而言,在查询到达之前,我将其拆分了: /p>
Well, you should use some logging to find out what's actually being sent to the database - but personally I'd split the query up before it gets there:
public ActionResult Search(string q)
{
var result = string.IsNullOrEmpty(q) ? storeDB.Products
: storeDB.Products.Where(p => p.Title.Contains(q));
return View(result);
}
可能是SQL CE支持的SQL方言不支持您正在使用的空性检查-这样就解决了这个问题.
It's possible that the SQL dialect supported by SQL CE doesn't support the check for emptiness that you were using - and this gets round that problem.
这篇关于这个linq查询出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!