简体   繁体   English

(obj!= null)不起作用

[英](obj != null) doesn't work

It seams simple but I really don't get it.I am using linq and Entity Framework to retrieve an object from Database by a simple query like this 它接缝简单,但我真的不明白。我正在使用linq和Entity Framework通过这样的简单查询从数据库中检索对象

loggedinUser = (from user in context.Users
                 where user == _guid
                 select user).ToList()[0];

I know that I can use .FirstOrDefault() , but I don't think that my problem has something to do with the way I get my user. 我知道我可以使用.FirstOrDefault() ,但我不认为我的问题与我获取用户的方式有关。

After getting user If I check it with this condition 获得用户后如果我检查这个条件

if (loggedinUser != null)
   {
     ToLocation = String.Format("{0} {1} {2} {3}", loggedinUser.StreetAddress,loggedinUser.City, loggedinUser.Province, loggedinUser.PostalCode);
   }

it doesn't work, as if it is null , but it's not.when I use this condition it works. 它不起作用,好像它是null ,但它不是。当我使用这个条件时它起作用。

if (loggedinUser == null)
{ }
else
{
  ToLocation = String.Format("{0} {1} {2} {3}", loggedinUser.StreetAddress,loggedinUser.City, loggedinUser.Province, loggedinUser.PostalCode);
}

I always use this type of condition (Obj != null) and it works,the answer should be simple, but I don't really get why it doesn't this time. 我总是使用这种类型的条件(Obj != null)并且它可以工作,答案应该很简单,但我不知道为什么它不会这次。 Am I missing something? 我错过了什么吗?

The only point is that the class for this entity is located in another project.Can it be the problem? 唯一的一点是这个实体的类位于另一个项目中。可能是问题吗?

The project which this class is located in is in VB.Net : 这个类所在的项目是在VB.Net中:

<Table("Users")>
Public Class User
<Key()>
Public Property UserID As Integer

Public Property Username As String
Public Property PasswordEncrypted As String
Public Property LastLogin As DateTime 
Public Property CreatedByUserID As Integer
Public Property DateCreated As DateTime
Public Property Deleted As Boolean '?
Public Property Email As String
Public Property StreetAddress As String
Public Property City As String
Public Property Province As String
Public Property PostalCode As String

<NotMapped()>
Public Property Lat As Double

<NotMapped()>
Public Property Lon As Double

Public Property GUID As String

<NotMapped()>
Public Property EULAAgreed As DateTime

Public Overrides Function ToString() As String
    Return Username
End Function

Public Overrides Function Equals(ByVal obj As Object) As Boolean
    If (obj Is System.DBNull.Value) Then
        Return MyBase.Equals(obj)
    ElseIf (TypeOf obj Is String) Then
        Return MyBase.Equals(obj)
    Else
        Try
            Return UserID = CType(obj, Entities.User).UserID
        Catch ex As Exception
            Return UserID
        End Try
    End If
End Function

End Class 结束班

It's because LINQ-SQL/EF don't give you null. 这是因为LINQ-SQL / EF不会给你null。 They give you DBNull. 他们给你DBNull。

Change your comparison to this: 将您的比较更改为:

if (loggedinUser != DBNull.Value)

It's possible that you have code like this: 你可能有这样的代码:

if (loggedinUser != null)
    doSomething();
    doSomethingElse();

The problem is that the if 's scope ends with the first statement. 问题是if的范围以第一个语句结束。 Include braces if you have multiple statements. 如果您有多个语句,请包括大括号。

if (loggedinUser != null)
{
    doSomething();
    doSomethingElse();
}

Although you got the answer for your question. 虽然你得到了你的问题的答案。 but still these answers also worth a read. 但这些答案仍然值得一读。

Well, null is not an instance of any type. 好吧,null不是任何类型的实例。 Rather, it is an invalid reference. 相反,它是一个无效的引用。

However, System.DbNull.Value, is a valid reference to an instance of System.DbNull (System.DbNull is a singleton and System.DbNull.Value gives you a reference to the single instance of that class) that represents nonexistent* values in the database. 但是,System.DbNull.Value是对System.DbNull实例的有效引用(System.DbNull是单例,System.DbNull.Value为您提供对该类的单个实例的引用),表示不存在的*值数据库。

*We would normally say null, but I don't want to confound the issue. *我们通常会说null,但我不想混淆这个问题。

So, there's a big conceptual difference between the two. 所以,两者之间存在很大的概念差异。 The keyword null represents an invalid reference. 关键字null表示无效引用。 The class System.DbNull represents a nonexistent value in a database field. System.DbNull类表示数据库字段中不存在的值。 In general, we should try avoid using the same thing (in this case null) to represent two very different concepts (in this case an invalid reference versus a nonexistent value in a database field). 通常,我们应该尝试避免使用相同的东西(在本例中为null)来表示两个非常不同的概念(在这种情况下,无效的引用与数据库字段中的不存在的值)。

Keep in mind, this is why a lot of people advocate using the null object pattern in general, which is exactly what System.DbNull is an example of. 请记住,这就是为什么很多人提倡一般使用null对象模式 ,这正是System.DbNull的一个例子。

Original Source 原始来源

and a very good explanation by Marc in What is the point of DBNull? Marc的一个非常好的解释是DBNull有什么意义?

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

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