包含多列查询

编程入门 行业动态 更新时间:2024-10-26 08:27:11
本文介绍了包含多列查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当任一列可能为空时,如何使用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;

更多推荐

包含多列查询

本文发布于:2023-10-14 11:48:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1490971.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!