linq NullReferenceException while checking for null reference

Posted by Odrade on Stack Overflow See other posts from Stack Overflow or by Odrade
Published on 2010-05-10T19:48:54Z Indexed on 2010/05/10 19:54 UTC
Read the original article Hit count: 285

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about linq-to-objects

Related posts about c#