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

celmahentity-frameworklinq

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;
        }
    }

Best Answer

The proper use would be

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

Related Topic