简体   繁体   English

是否应引用null变量引发异常

[英]Should a reference to a null variable throw exception

The query below consists of multiple INNER JOIN clauses. 下面的查询由多个INNER JOIN子句组成。

The Student table has a lot of foreign keys which are substituted by names in the query. Student表中有很多外键,这些外键被查询中的名称替换。 I am using LEFT OUTER JOIN query below. 我在下面使用LEFT OUTER JOIN查询。 Please note MarkID in the Students can be null . 请注意, Students MarkID可以为null That means marksGroup.DefaultifEmpty() may return null as well. 这意味着marksGroup.DefaultifEmpty()也可能返回null Therefore if I put only m instead of m != null ? m.MarkName : "-- Not marked --" 因此,如果我只放置m而不是m != null ? m.MarkName : "-- Not marked --" m != null ? m.MarkName : "-- Not marked --" in the last line, the code IMO should throw an exception when m is null because I am referring to this variable but it didn't. m != null ? m.MarkName : "-- Not marked --"在最后一行,当m为null时,代码IMO应该引发异常,因为我引用的是此变量,但不是。 On a example which I build this query on, a compiler thrown exception. 在构建此查询的示例上,编译器引发了异常。 Please see the code: 请查看代码:

var result = from st in dbContext.Students where st.DepartmentID == 17
             join d in dbContext.Departments on st.DepartmentID equals d.DepartmentID
             join sv in dbContext.SoftwareVersions on st.SoftwareVersionID equals sv.SoftwareVersionID
             join stat in dbContext.Statuses on st.StatusID equals stat.StatusID
             join m in dbContext.Marks on st.MarkID equals m.MarkID into marksGroup
             from m in marksGroup.DefaultIfEmpty()
             select new
             {
                 student = st.StudentName,
                 department = p.DepartmentName,
                 software = sv.SoftwareVersionName,
                 status = st.StatusName,
                 marked = m != null ? m.MarkName : "-- Not marked --"
             };

What am I missing. 我想念什么。 I am still beginner in terms of C# and LINQ . 我仍然是C#LINQ的初学者。

When you execute a LINQ statement against a SQL backend (I assume you have Entity Framework here) the whole statement is translated into SQL and executed by the database engine. 当您对SQL后端执行LINQ语句时(假设您在这里拥有Entity Framework), 整个语句将转换为SQL并由数据库引擎执行。

So this is what the .Net runtime does: 因此,.Net运行时将执行以下操作:

  • Translate the statement (which is an Expression ) into SQL 将语句(是Expression )转换为SQL
  • Send the SQL to the database through ADO.Net. 通过ADO.Net将SQL发送到数据库。
  • Receive the SQL result set (essentially an array of rows, where a row is itself an array of values). 接收SQL结果集(本质上是一个行数组,其中行本身就是值数组)。
  • Create objects from these rows. 从这些行创建对象。

In your case, the objects to be created can safely be defined by the part 在您的情况下,可以通过零件安全地定义要创建的对象

select new
{
    student = st.StudentName,
    department = p.DepartmentName,
    software = sv.SoftwareVersionName,
    status = st.StatusName,
    marked = m.MarkName
}

The only thing the .Net runtime is concerned with is to create an instance of an anonymous type and set its properties from one row in the result set. .Net运行时唯一关心的是创建一个匿名类型的实例,并从结果集中的一行中设置其属性。 In some rows, the value for marked may be null , so marked will set to null in that particular object. 在某些行中, marked的值可能为null ,因此在该特定对象中marked将设置为null The .Net runtime is not dealing with a MarksGroup object there. .Net运行时不在那里处理MarksGroup对象。

Of course in LINQ to objects this would be totally different. 当然,在LINQ中,对象将完全不同。 There the null check is absolutely necessary to prevent null object references. 为防止空对象引用,绝对必须执行空检查。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM