0

I have a model, in the model a method such as,

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

If I pass the wrong parameter (like 123333-no record it database), it throws an exception,

Sequence contains no elements

What is the correct code version, or can try and catch simply be used?

1 Answer 1

5

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.