包含多列查询 [英] Contains Query on multiple columns

查看:84
本文介绍了包含多列查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当任一列可能为空时,如何使用LINQ to SQL搜索多个列?

How do I search multiple columns using LINQ to SQL when any one of the column could be null?

IEnumerable<User> users = from user in databaseUsers
        where
             user.ScreenName.Contains(search)
             || user.FirstName.Contains(search)
             || user.LastName.Contains(search)
        select user;

我不断收到此异常:

对象引用未设置为 对象的实例.

Object reference not set to an instance of an object.

说明:一个 期间发生未处理的异常 当前网站的执行情况 要求.请检查堆栈跟踪 有关该错误的更多信息 以及它在代码中的起源.

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.NullReferenceException:对象 引用未设置为的实例 一个对象.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

推荐答案

添加非空条件user.Property != null

  IEnumerable<User> users = from user in databaseUsers
    where
         (user.ScreenName != null && user.ScreenName.Contains(search))
         || (user.FirstName != null && user.FirstName.Contains(search))
         || ( user.LastName != null && user.LastName.Contains(search))
    select user;

这篇关于包含多列查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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