Error: Sequence contains more than one element
I am getting the following err开发者_Go百科or on production server. It works well on localhost.
Error: Sequence contains more than one element
That is an InvalidOperationException
thrown by the Single
method.
This method is supposed to return only one element, and your query is returning more than one, you have to check the criteria you use on your query.
It will also throw an exception if it doesn't find an element. You can use SingleOrDefault()
method to return null (or default value, i.e. 0 for int) if there are no records.
If you are expecting multiple elements but just one the first one, you can use First
instead of Single
or FirstOrDefault
.
精彩评论