asp.net - Linq lambda expression throwing error if result set is null -


in project need select 1 row values using linq

try { return database.employees.where(x => id.equals(x.id)).single(); } catch (invalidoperationexception ix) {     throw; } 

using corresponding row values through entity object.but throwing error if result set empty.the problem in project exception must logged. how can manage code out going exception.

you're using single() documented throw exception if there no results. if don't want behaviour, don't use method :)

if use singleordefault() return null if there no results. still throw exception if there multiple results, however. alternatively use firstordefault avoid this.

note can specify predicate in single / singleordefault well, don't need where call first:

return database.employees.singleordefault(x => id.equals(x.id)); 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -