linq NullReferenceException while checking for null reference
- by Odrade
I have the following LINQ query:
List<FileInputItem> inputList = GetInputList();
var results = from FileInputItem f in inputList
where ( Path.GetDirectoryName(f.Folder).ToLower().Trim() == somePath
|| Path.GetDirectoryName(f.Folder).ToLower().Trim() == someOtherPath )
&& f.Expression == null
select f;
Every time this query is executed, it generates a NullReferenceException. If I remove the condition f.Expression == null or change it to f.Expression != null, the query executes normally (giving the wrong results, of course).
The relevant bits of FileInputItem look like this:
[Serializable]
public class FileInputItem
{
[XmlElement("Folder")]
public string Folder { get; set; }
[XmlElement("Expression")]
public string Expression { get; set; }
/*SNIP. Irrelevant properties */
}
I'm new to LINQ to objects, so I'm probably missing something fundamental here. What's the deal?