tags:

views:

104

answers:

4

Hi all,

I'm trying to compare a string field from a LINQ query on a database using:

e.Comment.equals("Working From Home")

on the WHERE clause.

However, sometimes the Comment field could be empty which currently causes an Object reference not set to an instance of an object exception.

Is there any way I can check if the Comment isn't empty and THEN compare to avoid the exception?

A: 
!String.IsNullOrEmpty(e.Comment) && e.Comment == "Working From Home"
Andrew
While I'm not sure .NET will understand how to translate `IsNullOrEmpty` into SQL if he is using LINQ to SQL, the `==` will do both of the jobs done by `IsnullOrEmpty`.
Yuriy Faktorovich
+1  A: 

In this case, it should be enough to use the '==' operator instead:

e.Comment == "Working From Home"
jeroenh
+4  A: 

You can use == instead of Equals:

e.Comment == "Working From Home"

LINQ to SQL will correctly translate this to the appropriate SQL syntax.

Mark Byers
Makes perfect sense, didn't even think about it being so simple. Thanks!
Eton B.
A: 

You could do "Working From Home".Equals(e.Comment); or String.Equals(e.Comment, "Working From Home");

The Josenator