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

c# elmah entity-framework linq

Question

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() is suppose to "Determine weather a sequence contains any elements", so why the exception "System.InvalidOperationException Sequence contains no 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

p.s.: Can't reproduce the error but it shows up on Elmah error logs sometimes.

Below is some of 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;
        }
    }
1
4
10/4/2012 3:34:05 PM

Accepted Answer

The proper use would be

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

0
10/4/2012 2:58:51 PM

Popular Answer

I had exactly the same problem.

Was using the method Any() but the StackTrace shows error on the Single() method.

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

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow