简体   繁体   English

handle null query c# when equals null, Object reference not set to an instance of an object

[英]handle null query c# when equals null, Object reference not set to an instance of an object

I have a problem when converting sql to query c# about left join here is sql query我在将 sql 转换为查询 c# 时遇到问题,这里是 sql 查询

select mTeacher.Id as Id, mTeacher.Name as Name, mStudents.Name as Addresses
from Teachers mTeacher
 left join Students mStudents
    on mStudents.TeacherId=mTeacher.Id
    where mStudents.Name = 'some of word'

here is the image这是图像

and here is i converted to sql to query c#这是我转换为 sql 以查询 c#

 var zzz= from mTeacher in repo.Teachers
                      join mStudents in repo.mStudents on mTeacher.Id equals mStudents.TeacherId into a
                      from y1 in a.DefaultIfEmpty()
                      where mTeacher.Name.Equals("someofword") or mStudent.Name.Equals("somofword")
                      select new { mTeacher.Id,mTeacher.Name};

there will be student will have null value mStudent.Name.Equals("somofword") i got something like this how to handle this会有学生会有 null 值mStudent.Name.Equals("somofword")我得到了这样的东西如何处理

If I go by the SQL query, your where clause is only looking up Student.Name .如果我通过 SQL 查询 go ,则您的 where 子句仅查找Student.Name

This has to be not null (as per you where query).这必须不是 null (根据您的查询)。 So I would suggest that you use simply an inner join.因此,我建议您仅使用内部联接。 This will solve your C# conversion issue as well.这也将解决您的 C# 转换问题。

If you still want to retain the check, then you can modify your where clause to:如果您仍想保留支票,则可以将 where 子句修改为:

where mTeacher.Name.Equals("someofword") or (mStudent != null && mStudent.Name.Equals("somofword"))

The null check will make sure that you do not run into the object is null error (remember in case of left join the student value can be null). null 检查将确保您不会遇到 object 是 null 错误(请记住,在左连接的情况下,学生值可以为空)。

Also add a similar null check in your select clause, if you are referring to values from student table.如果您指的是学生表中的值,请在您的 select 子句中添加类似的 null 检查。

 select new { mTeacher.Id,mTeacher.Name, StudentName = ( y1 == null ) ? "No Student" : y1.Name};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#递归方法返回空引用,对象引用未设置为对象的实例。 XElement对象 - C# Recursive method returning null reference, object reference not set to an instance of an object. XElement Objects C#对象引用未设置为对象的实例(在堆栈跟踪中未提及空引用) - c# object reference not set to an instance of an object (no mention of null reference in stack trace) 空引用:对象引用未设置为对象的实例 - Null Reference : Object reference not set to an instance of an object 在SQL查询中未将对象设置为对象的实例,空引用异常 - Object not set to an instance of an object in sql query, null reference exception “对象引用未设置为对象的实例”即使我检查了 null - "Object reference not set to an instance of an object" even when I checked null 对象引用未设置为检查null的对象实例 - object reference not set to an instance of an object in check for null 未将对象引用设置为对象的实例,null - Object reference not set to an instance of an object, null “对象引用未设置为对象的实例”-但是没有什么是空的? - “Object reference not set to an instance of an object” - but nothing is null? 对象引用未设置为对象的实例(null DataSet ??) - Object reference not set to an instance of an object (null DataSet??) 未将对象引用设置为对象的实例,参数为null - Object reference not set to an instance of an object, parameter is null
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM