这个linq查询出了什么问题? [英] whats is wrong with this linq query?

查看:99
本文介绍了这个linq查询出了什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个产品表,其中的标题为字符串.

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

该函数的指定参数值无效. [参数#= 1,函数名称(如果已知)= isnull] 说明:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪,以获取有关错误及其在代码中起源的更多信息.

The specified argument value for the function is not valid. [ Argument # = 1,Name of function(if known) = isnull ] Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

异常详细信息: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 serach didn'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查询出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆