views:

149

answers:

2
    /// <summary>
    /// Find all of the Areas that are Parents without children Areas.
    /// </summary>
    /// <returns>IQueryable object full of Areas.</returns>
    public IQueryable FindParentAreas()
    {
        return db.Areas.SelectMany(x => x.ParentAreaID == null);
    }

I want to return a collection of Area objects that are parent Areas, meaning they have null values in the ParentAreaID field (in my database).

It seems I can't just compare it to null because null in C# maybe means something else in Microsoft SQL Server.

Any guidance? Should I even be using SelectMany?

+2  A: 

Enumerable.SelectMany:

Projects each element of a sequence to an IEnumerable<T> and flattens the resulting sequences into one sequence.

Enumerable.Where:

Filters a sequence of values based on a predicate.

I think you're looking for Where:

public IQueryable<Area> FindParentAreas()
{
    return db.Areas.Where(x => x.ParentAreaID == null);
}
dtb
Thank you! However, I'm still receiving the error - 'the type arguments cannot be infered.' It appears I need to be more explicit about 'null' but I don't know how.
Sergio Tapia
Nevermind - I hit ctrl+shift+B to rebuild and now it gives me no errors. Thanks!
Sergio Tapia
A: 

I think you want to use Where:

return db.Areas.Where(x => x.ParentAreaID == null)
minimalis