views:

54

answers:

1

i have model, in model such method

public Pages GetPage(int? id)
    {            
        return _dataContext.Pages.First(p => p.id == id);
    }

if i pass whong parameter (like 123333-no record it database), it throw exception

Sequence contains no elements

can you suggest correct code version, or simply use try\catch?

+5  A: 

Assuming Pages is a reference type, in which case default<T> is null:

public Pages GetPage(int? id)
{
    return _dataContext.Pages.FirstOrDefault(p => p.id == id);
}
Lachlan Roche