Error message: Sequence contains no elements
-
08-12-2019 - |
Question
I am using Connection.db.Single<Test>("select * from Test WHERE ID= " + id)
for a test as follows:
Assert.IsNull(Connection.db.Single<Test>("select * from Test WHERE ID= " + id));
I know that the resultset should be a null as there is no row for the value of id I am passing. However, an error is fired as follows:
Sequence contains no elements and exception details is as follows: System.InvalidOperationException was unhandled by user code Message=Sequence contains no elements Source=System.Core StackTrace: at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source) at PetaPoco.Database.Single[T](String sql, Object[] args) in C:\Dev\Models\PetaPoco.cs:line 1120
- Can`t I use Single to test null?
- Is there a better method to test the Asset.IsNUll ?
Solution
Use:
Assert.IsNull(Connection.db.SingleOrDefault<Test>("select * from Test WHERE ID= " + id));
OTHER TIPS
use Any() or FirstOrDefault, as in:
if ( context.Product.Any( x => x.ProductId == 1 ) ) ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow