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

  1. Can`t I use Single to test null?
  2. Is there a better method to test the Asset.IsNUll ?
Was it helpful?

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
scroll top