ToListAsync() fails on ASP NET CORE but ToList() works - Error "Sequence contains no elements."

I get a list like:

var test = await list.ToListAsync();

this function works fine with ASP.NET MVC but with the migration to NET CORE I have the error "Sequence contains no elements.

The list objest is an IOrderedQueryable.

When I execute the sentence:

var test = list.ToList();

works correctly but I need a Async method...

EntityFrameworkCore Version: 2.1.1

Any idea of the problem? Thank you.

2 answers

  • answered 2018-07-12 07:51 Abubakar Iftikhar

    Hopefully it will get you the required list.

    Your interface signature would be like this

    Task<ICollection<T>> GetAllAsyn();

    Your implementation would be like this.

    public virtual async Task<ICollection<T>> GetAllAsyn()
       return await _context.Set<T>().ToListAsync();

  • answered 2018-07-12 07:57 Carlos Tenorio

    I found the issue. I get the list with a select that fails because a MAX() inside the select fails.

    Max() issue (Only happends with EntityFrameworkCore ): -> Stackoverflow: Max return value if empty query