Another System.InvalidOperationException: The sequence contains no elements (using .Any ())

Getting the exception on this line:

public bool isEngageOn() { line 149 -> return chatUserRepository.Table.Where(c => c.TrackingOn).Any(); } 

TrackingOn is of type boolean.

.Any () assumes "Determine the weather, the sequence contains any elements", so why the exception "System.InvalidOperationException Sequence does not contain elements" caught on Elmah?

Error:

 System.InvalidOperationException: Sequence contains no elements at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source) at System.Linq.Queryable.Any[TSource](IQueryable`1 source) at sf2015.Models.DomainModels.Services.ChatServices.isEngageOn() in C:\....\ChatServices.cs:line 149 

ps: Cannot reproduce the error, but sometimes appears in Elmah error logs .

Below is the code for the repository

 public virtual IQueryable<T> Table { get { return this.Entities; } } private DbSet<T> Entities { get { if (_entities == null) _entities = Context.Set<T>(); return _entities; } } 
+4
source share
3 answers

Proper use will be

return chatUserRepository.Table.Any(c => c.TrackingOn)

0
source

The stack trace you presented says the exception is from .Single , which raises the specified exception if no items are found. You can change the specified code to:

  return chatUserRepository.Table.Any(c => c.TrackingOn); 

Which will do the same as your string.

0
source

I had exactly the same problem.

Used the Any() method, but StackTrace shows an error in the Single() method.

The cause of the problem was the Seed () method in the Entity Framework First Configuration class.

My Seed() method used the Single() method on a table that has no registries.

0
source

Source: https://habr.com/ru/post/1437835/


All Articles