开发者

ASP.NET MVC select question

开发者 https://www.devze.com 2022-12-21 07:16 出处:网络
I have a model, in the model a method such as, public Pages GetPage(int? id) { return _dataContext.Pages.First(p => p.id == id);

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 conta开发者_StackOverflow中文版ins no elements

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


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消