对象引用未设置为 LINQ C# 中对象的实例
Object reference not set to an instance of an object in LINQ C#
我有一个名为 dtMondayNewsLetter
的数据表,当 DT returns 没有行时。 i.e. Empty
it shows an error Object reference
not set to an instance of an object
代码
CurrentDirection = Convert.ToString((from DataRow dr in dtMondayNewsLetter.Rows
where (int)dr["PostID"] == PostID select (string)dr["Direction"])
.FirstOrDefault()).Trim();
我需要对上面的代码做哪些更改才能消除错误。
如你所说,查询
from DataRow dr in dtMondayNewsLetter.Rows
where (int)dr["PostID"] == PostID
select (string)dr["Direction"]
returns 没有行,所以 FirstOrDefault()
returns null
.
因此您可以简单地 if
检查结果是否可用..
var resultString = (from DataRow dr in dtMondayNewsLetter.Rows
where (int)dr["PostID"] == PostID
select (string)dr["Direction"])
.FirstOrDefault();
if (resultString != null)
{
CurrentDirection = resultString.Trim();
}
else
{
//case, when no rows
}
您可以使用 Null-Coalescing Operator。如果 FirstOrDefault
return 为 null return 空字符串。
CurrentDirection =
Convert.ToString(
(from DataRow dr in dtMondayNewsLetter.Rows
where (int) dr["PostID"] == PostID
select (string) dr["Direction"]).FirstOrDefault() ?? "").Trim();
为了扩展我的评论,我会做类似的事情:
foreach (DataRow dr in dtMondayNewsLetter.Rows)
{
if (dr["PostID"] != DBNull.Value && dr["Direction"] != DBNull.Value)
{
if ((int)dr["PostID"] == PostID)
{
CurrentDirection = dr["Direction"].ToString();
}
}
}
通过将所有内容都塞进 LINQ 语句来尝试简洁并不总是最佳的。在您的情况下,可能存在多个对象或数据,即NULL
,从而导致空引用异常。
在一行 LINQ 语句中,很难找出 NULL 对象的位置。将该 LINQ 语句拆分为 for...each
循环使事情变得容易得多,并获得完全相同的结果。
但是,如果 "PostID" 和 "Direction" 是不可空的(因此不需要使用 DBNull.Value 检查),那么使用@nopeflow 答案,因为这比你的更安全当前实施。
我有一个名为 dtMondayNewsLetter
的数据表,当 DT returns 没有行时。 i.e. Empty
it shows an error Object reference not set to an instance of an object
代码
CurrentDirection = Convert.ToString((from DataRow dr in dtMondayNewsLetter.Rows
where (int)dr["PostID"] == PostID select (string)dr["Direction"])
.FirstOrDefault()).Trim();
我需要对上面的代码做哪些更改才能消除错误。
如你所说,查询
from DataRow dr in dtMondayNewsLetter.Rows
where (int)dr["PostID"] == PostID
select (string)dr["Direction"]
returns 没有行,所以 FirstOrDefault()
returns null
.
因此您可以简单地 if
检查结果是否可用..
var resultString = (from DataRow dr in dtMondayNewsLetter.Rows
where (int)dr["PostID"] == PostID
select (string)dr["Direction"])
.FirstOrDefault();
if (resultString != null)
{
CurrentDirection = resultString.Trim();
}
else
{
//case, when no rows
}
您可以使用 Null-Coalescing Operator。如果 FirstOrDefault
return 为 null return 空字符串。
CurrentDirection =
Convert.ToString(
(from DataRow dr in dtMondayNewsLetter.Rows
where (int) dr["PostID"] == PostID
select (string) dr["Direction"]).FirstOrDefault() ?? "").Trim();
为了扩展我的评论,我会做类似的事情:
foreach (DataRow dr in dtMondayNewsLetter.Rows)
{
if (dr["PostID"] != DBNull.Value && dr["Direction"] != DBNull.Value)
{
if ((int)dr["PostID"] == PostID)
{
CurrentDirection = dr["Direction"].ToString();
}
}
}
通过将所有内容都塞进 LINQ 语句来尝试简洁并不总是最佳的。在您的情况下,可能存在多个对象或数据,即NULL
,从而导致空引用异常。
在一行 LINQ 语句中,很难找出 NULL 对象的位置。将该 LINQ 语句拆分为 for...each
循环使事情变得容易得多,并获得完全相同的结果。
但是,如果 "PostID" 和 "Direction" 是不可空的(因此不需要使用 DBNull.Value 检查),那么使用@nopeflow 答案,因为这比你的更安全当前实施。